Dart

What is Dart Programming

Dart is an open-source, general-purpose and object-oriented programming language which is C++ styled syntax developed by Google. The purpose of Dart programming is to create a frontend user interface for the web, mobile and desktop apps. It is actively developed and is compiled to native machine code for building apps, inspired by other programming languages such as Java, JavaScript, C#, and is Strongly Typed and null safe. Since Dart is a compiled language so you cannot execute your code directly; instead, it is compiled and transfers it into machine code.

It works with the core concepts of programming languages like classes, interfaces and functions. Dart language does not support arrays but you can use lists instead. It supports collection, which is used to replicate the data structure such as List,Maps, generics, and optional typing.

The following example shows simple Dart programming.
void main() {
   print('hello world');   
} 

Data Type

Dart is a Strongly Typed programming language similar to classic languages like C/C++/Java. Recently dart developers have added null safety. Which means, each value you use in your programming language you have to declare the type is it a string or an int when the code is compiled. Here, we are going to discuss the basic data types of Dart programming language.
Data Type Example Descriptions
String String myName =
MyName;
It holds the string data. You can use single(‘) or double(“) quotes. You should open and close with the same quotation mark .
num, int, double int age = 25;
double price =;7UIILL;
46
125.50;
The num data type stands for number. It Have two parts:

  • Integer (It contains whole numbers without any decimal)
  • Double (Numbers with decimals)
Boolean bool var_name =
true;
Bool means true or false. This data type is used when you want to get a true or false answer.
object Person =
Person()
In Dart is an object (e.g., Integer, String). But you can create your own complex objects using classes.

Variables and Functions

Variables are the namespace which associates with the memory that stores values you need to use in the program. Variables are called identifiers. They contain data, which stores values of any type. For example:

var name = Adam;

Here, name is a variable that stores a string of value Adam. We can declare with String instead of var. Dart has a property named Type Inference, which helps infer the type of the variable according to the values. So, if we create a variable starting with a var keyword instead of a name keyword, Dart can infer its type according to the value associated with it.

Functions are another core feature of dart language. Functions are a group of statements that performs a particular task. These are organized in a logical block of code that are readable, maintainable, and reusable. The function declaration contains the return type, function name and parameters which are passed by the parent to get the output in the return type. The following example explains a function that is used in a Dart program.

//type name(args,..){
		//return;
//}
//Function declaration   this will return a num type so we can get int or double. 
num sumNumbers(num a, num b) {  
    // Here, we use num as a type so we can pass int or double.  
    return a + b;  
}  
void main(){
var price1 = 29.99;  
var price2 = 20.81;  
var sum = sumNumbers(price1, price2);  
var num1 = 10;  
var num2 = 45;  
var sum2 = sumNumbers(num1, num2);
} 

Operators

Dart language supports various operators similar to other programming languages such as C++,Java, Kotlin and Swift. The operators are listed below:

  • Arithmetic (+, -, /, *)
  • Equality (==, !=)
  • Increment and Decrement(++, –)
  • Logical (&&, ||)
  • Comparison(>,<,>=,<=,)

Decision Making and Loops

Decision making is one of the biggest requirements in any programming language. We use decisions to differentiate between different conditions. In Dart language we can use these statements for decision making:

  • If statement
  • If-else statement
  • Ternary statement
  • Switch statement
The below diagram explains it more clearly.

flutter-dart-diagram

Example

//If else

void main() {   
   var num = 12;   
   if (num % 2 = = 0) {   
      print("Number is Even.");   
   } else {   
      print("Number is Odd.");   
   }
}

//Ternary operators

void main() {   
   var num = 12;   
   num % 2 = = 0?print("Number is Even."):print("Number is Odd.");   
} 

Loops are used when you want to execute a block or a single line of code repeatedly until a specified condition is met. Dart language supports the following types of loop statements:

for //

 for(var i=0;i<10;i++){
print(i);
}

for..in //

var data =[“Amit”,”Anuj”,”Santosh”];
for (var val in data){
	print(val);
  }

while//

var i=0; 
 while(i<10){
print(i);
i++
}

do..while//

var i=0;  
do{
print(i);
i++
}while(i<0);
//This code will run atleast once 

The below diagram explains it more clearly.

flutter-dart-loop-diagram

Comments

Comments are the lines of which the compiler ignores so these are not executed. They are one of the most important aspects of a programming language. Comments are used to provide information about the function, variable, or the operation we are performing. There are the various type of comments used in Dart Language:

  • Make format comments: Single line comment (//) e.g. //This is a comment
  • Block Comments: These are the multi-line comments (/*...*/) e.g. /*This is a comment*/
  • Doc Comments: Document comments that used for defining members and types of a function (///) ///a:variable 1

Continue and Break

Sometimes we want to skip some particular condition or want to exit when some condition is met. So for doing this we have to continue and break keywords in Dart. Continue is used to skip a condition. It will be clarified by the example

Example
void main() {   
  for(int i=0;i<=10;i++){    
    if(i==5){    
      continue; //it will skip the rest statement           
}    
    print(i);    
  }   
}  
// This will print 0,1,2,3,4,6,7,8,9,10

Break is used to break the loop if some condition is met. It will be clarified by the example

Example
void main() {   
  for(int i=0;i<=100;i++){    
    if(i==10){
         break;
     }
    print(i);    
  }   
}  
// This will print 0,1,2,3,4,5,6,7,8,9  

Final and Const Keyword

There are some keywords which can be used to restrict the user from changing the value of the variable. Final is used when you want to restrict the value but don’t want to declare from the start.

Example
b=10;
void check(int b) {   
  final a = b;//a=10 
b=20; 
  print(a);//10
print(b);//20
a=10;//Not Allowed. Will give an error   
}

Const keyword is used to declare a constant value. We can’t change the value of the const keyword after assigning it. We have assigned the value at the time of declaration.

Example
void main() {     
  const pi = 3.14;
  print(pi);  
}  

Object-Oriented Programming

Dart is an object-oriented programming language, which means everything is an object in dart. A num is an object. Dart programming uses the core concept of OOPs like objects, classes, interfaces,inheritance, etc.

Object: An object is any entity, which has a state and a behavior associated with it. Objects can be physical or logical . In Dart, everything is an object, even primitive data types like String and num are objects. Dart allows you to build your custom object which can be used to represent a more complex relationship between data.

Class: A class is a blueprint based on which we create an object. It means we create objects using classes as the blueprint. A class definition contains the following:

  • Fields
  • Methods
  • Constructor
  • Getters and setters

Let us see an example, for understanding the basic OOPs concepts.

class Dog {  
  // Property Declaration  
  String color, breed, name;  
    
  // Method Creation  
  String speak() {  
    return "Bow Bow";  
  }  
  String run() {  
    return "${this.name} started running.";  
  }  
}  
  
void main() {  
  // Object Creation  
  var myDog = new Dog();   
    
  // Accessing Class's Property  
  myDog.color = "Black";   
  myDog.name = "Tiger";  
  myDog.breed = "Husky";  
    
  //Display Output  
  print(myDog.color);  
  print(myDog.name);  
  print(myDog.breed);  
  print(myDog.speak());  
  print(myDog.run());    
}  

In the above example, we define a class Dog, which has three variables of string type and two methods. We used the main function which is the starting for Dart execution. Inside the main, we created an object which helps us access the class's properties. Finally, we print the output.

Subscribe Now