Dart is an open-source general-purpose programming language. It was firstly developed by Google. Dart is an object-oriented language with C- style syntax. It supports programming generalities like interfaces, and classes, unlike other programming languages Dart does not support arrays.

Dart collections can be used to replicate data structures similar to arrays, generics, and optional typing.

The following code shows a simple Dart program

void main() {
   print("Dart language is easy to learn");
}

Variables and Datatypes

Variable is named storage location and Data types simply refer to the type and size of data associated with variables and functions.

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

var name = 'Rakesh Modi';

The final and const keyword are used to declare constants. They are defined as below

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

Dart language supports the following data types :

  1. Number
  2. Strings
  3. Booleans
  4. List & Maps
  5. Dynamic

1.Number : Integer & Double

2.String : It represents a sequence of characters. String values are specified in either single or double quotes.

3.Booleans : True & False

4.List & Maps :

1.List : collection of multiple values

Example :-

void main() {
  var list = [101,201,301,401,501];
  print(list);
}

2.Maps : Collection of objects

Example :-

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

5.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 = "Hiren";
   print(name);
}

Decision Making and Loops

A decision-making block evaluates a condition before the instructions are executed. Dart supports If, If..else and switches statements.

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

Example :-

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

Functions

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

Example :-

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

Object Oriented Programming

Dart 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
class Employee {
   String name;
   
   //getter method
   String get emp_name {
      return name;
   }
   //setter method
   void set emp_name(String name) {
      this.name = name;
   }
   //function definition
   void result() {
      print(name);
   }
}

void main() {
   //object creation
   Employee emp = new Employee();
   emp.name = "Vivek";
   emp.result(); //function call
}