Flutter - Introduction To Dart Programming
Flutter - Introduction To Dart Programming
void main() {
print("Dart language is easy to learn");
}
Dart uses var keyword to declare the variable. The syntax of var is defined below,
The final and const keyword are used to declare constants. They are defined as below −
void main() {
final a = 12;
const pi = 3.14;
print(a);
print(pi);
}
void main() {
var list = [1,2,3,4,5];
print(list);
}
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);
}
Loops are used to repeat a block of code until a specific condition is met. Dart supports
for, for..in , while and do..while loops.
https://www.tutorialspoint.com/flutter/flutter_introduction_to_dart_programming.htm 2/4
12/23/23, 4:24 PM Flutter - Introduction to Dart Programming
Let us understand a simple example about the usage of control statements and loops −
void main() {
for( var i = 1 ; i <= 10; 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 here −
void main() {
add(3,4);
}
void add(int a,int b) {
int c;
c = a+b;
print(c);
}
The above function adds two values and produces 7 as the output.
A class is a blueprint for creating objects. A class definition includes the following −
Fields
Constructors
https://www.tutorialspoint.com/flutter/flutter_introduction_to_dart_programming.htm 3/4
12/23/23, 4:24 PM Flutter - Introduction to Dart Programming
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 = "employee1";
emp.result(); //function call
}
https://www.tutorialspoint.com/flutter/flutter_introduction_to_dart_programming.htm 4/4