Flutter - Introdução À Programação em Dart
Flutter - Introdução À Programação em Dart
void main() {
Dart language is easy to learn
}
Dart uses the var keyword to declare variables. The syntax of var is defined below,
Dart
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.
Unable to access external links or websites. 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 perform 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
The provided input is a URL and cannot be translated. Please provide text for translation. 3/4
12/23/23 Flutter - Introduction to Dart Programming
Functions
class Employee {
String name;
//getter method
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();
employee1
emp.result(); //function call
}