3. Flutter Tutorial for Beginners – Basic about Dart Programming

0
569

Dart Structure:

The main() method of Dart script needs for execution. print() is a predefined function that is used to prints the value to the standard output.

main() { 
   print("Hello World!"); 
}

Date Types:

The Dart language supports the following types−

  • Numbers :
    • int: Integer values represent non-fractional values. For example:
int num = 10;
    • double: Double values represent fractional values. For example:
double num = 23.25;
  • Strings
    • String: Strings represent a sequence of characters. For example:
String name = 'Smith';
  • Booleans
    • Bool: The Boolean data type represents Boolean values true and false.
bool check = true;
  • Lists
    • List: The data types list is used to represent a collection of objects
List data = [{‘id’: ’1’, ’name’: ’Solaiman Ahmed’, ’country’: ’America’},
{‘id’: ’2’, ’name’: ’John Feric, ’country’: ’Russia’},
{‘id’: ’3’, ’name’: ’Chu Chan’, ’country’: ‘China}]
  • Maps
    • Map: The data types map is also used to represent a collection of objects
Map dataMapping = {
  'America': This is random dummy text,
'China': This is the value of China
};
  • Dynamic
    • dynamic: Dynamic type variable is used when a variable is not explicitly specified.
dynamic data = ‘Hello World!!!’

Loops:

The for loop is an implementation of a definite loop.

void main() { 
   var num = 5; 
   var factorial = 1;
   for( var i = num ; i >= 1; i-- ) { 
      factorial *= i ; 
   } 
   print(factorial); 
}

The foreach provides an easy way to iterate over array or list. Foreach is used to traverse each element of the list.

void main(){
    var myList = [24, 63, 84];
    myList.forEach((element) => 
        print(element)
    );
}

The while loop executes the instructions each time the condition specified evaluates to true.

void main() { 
   var num = 5; 
   var factorial = 1; 
   while(num >=1) { 
      factorial = factorial * num; 
      num--; 
   } 
   print("The factorial  is ${factorial}"); 
} 

Decision Making:

An if can be followed by an optional else block. The else block will execute if the Boolean expression tested by the if block evaluates to false.

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

Defining a Function:

A function definition specifies what and how a specific task would be done.

void function_name() { 
   //statements 
}

Class and objects:

A class in terms of OOP is a blueprint for creating objects. A class encapsulates data for the object. Dart gives built-in support for this concept called class.

Syntax:

class class_name {  
   #fields 
   #getters/setters 
   #constructors 
   #functions
}

Example:

class Student { 
   String name; 
   int age; 
    
   String get stud_name { 
      return name; 
   } 
    
   void set stud_name(String name) { 
      this.name = name; 
   } 
   
   void set stud_age(int age) { 
      if(age<= 0) { 
        print("Age should be greater than 5"); 
      }  else { 
         this.age = age; 
      } 
   } 
   
   int get stud_age { 
      return age;     
   } 
}  
void main() { 
   Student s1 = new Student(); 
   s1.stud_name = 'MARK'; 
   s1.stud_age = 0; 
   print(s1.stud_name); 
   print(s1.stud_age); 
}

That’s it for now…

LEAVE A REPLY

Please enter your comment!
Please enter your name here