Sun. Nov 3rd, 2024

Variable  can be called as storage location and Data types  refers to the type and size of data associated with functions and  variables.

The syntax of var is defined below(Dart uses var keyword to declare the variable),

var name = 'Dart';

The final and const keyword are used to declare constants like below −

void main() {
   final a = 11;
   const pi = 3.14;
   print(a);
   print(pi);
}

Dart  supports the following data types −

  • Numbers − It is used to represent numeric literals (Integer and Double).
  • Strings − It represents a sequence of characters. String can specified in either single or double quotes.
  • Booleans − Dart language uses the bool keyword to represent Boolean values – true and false.
  • Lists and Maps − Collection of objects can be defined as List and Maps A simple List can be defined as below −
void main() {
   var list = [1,2,3,4,5];
   print(list);
}

The list shown above produces [1,2,3,4,5] list.

Map can be defined as shown here −

void main() {
   var mapping = {'id': 1,'name':'Dart'};
   print(mapping);
}

Dynamic − If the variable type is not defined, then its default type is dynamic. The following example illustrates the dynamic type variable −

void main() {
   dynamic name = "Dart";
   print(name);
}

Decision Making and Loops

A decision making block can evaluate a condition before the instructions are executed. Dart supports If, If..else and switch statements.

Loops are used to repeat a part of code until a specific condition is met. Dart supports for, for..in , while and do..while loops.

for example about the usage of control statements and loops −

void main() {
   for( var i = 1 ; i <= 10; i++ ) {
      if(i%2==0) {
         print(i);
      }
   }
}

The above code prints the even numbers from 1 to 10.

Functions

A group of statements that together performs a specific task called as function. Let us look into a simple function in Dart as shown here −

void main() {
   add(6,4);
}
void add(int a,int b) {
   int c;
   c = a+b;
   print(c);
}

The above function adds two values and produces 10 as the output.

Object Oriented Programming

Dart language is an object-oriented language. It supports object-oriented programming features like classes, interfaces, etc.

A class is a blueprint for creating objects. A class definition includes the following −

  • Fields
  • Getters and setters
  • Constructors
  • Functions

Now, let us create a simple class using the above definitions −

class Student {
   String name;
   
   //getter method
   String get stud_name {
      return name;
   }
   //setter method
   void set stud _name(String name) {
      this.name = name;
   }
   //function definition
   void result() {
      print(name);
   }
}
void main() {
   //object creation
   Student stud = new Student();
   stud.name = "student1";
   stud.result(); //function call
}

By Rajashekar

I’m (Rajashekar) a core Android developer with complimenting skills as a web developer from India. I cherish taking up complex problems and turning them into beautiful interfaces. My love for decrypting the logic and structure of coding keeps me pushing towards writing elegant and proficient code, whether it is Android, PHP, Flutter or any other platforms. You would find me involved in cuisines, reading, travelling during my leisure hours.

Leave a Reply

Your email address will not be published. Required fields are marked *