Dart cheatsheet (Dart) - https://dart.dev/codelabs/dart-cheatsheet
Built-in types (Dart) - https://dart.dev/language/built-in-types
Common Data Types
int
double
String
you may use single quotes 'a' or double quotes "a"
use == to check the equality of two Strings
bool
List: [1,2,3]
Set: {1,2,3}
Map: {'a':1,'b':2,'c':3}
Dynamic Type var
Without initialization during declaration: You may change type of stored data during run-time
var x;
x = 32
x = 'abc'
With initialization during declaration: You CANNOT change type of stored data during run-time
var x = 32;
x = 'abc' //error
final Variables
Once the value is set, it cannot be changed.
It can be used alone or with other data type.
final s1 = 'abc';
final String s2 = 'abc';
const Variables
Compile-time constants
Differences between final vs const:
A final object cannot be modified but its fields can be changed; A const object is immutable thus its fields cannot be changed.
List a = [1,2];
a.add(3);
print(a); //print [1,2,3]
final List b = [1,2];
b.add(3);
print(b); //print [1,2,3]
const List c = [1,2];
c.add(3); //Error
print(c);
nullable Variables
By default, Dart does not allow you to assign a null value to a variable. If you want to set a variable to null, you have to declare the variable nullable by adding a question mark (?) at the end of the date type.
List a = null; //Error
List? b = null; //OK
Conditionals
if(condition1){
...
}else if(condition2){
...
}else{
...
}
Both else if and else statements are optional. You can have as many else if statements as you want.
Repetitions
for loop
for(var i = initial_value; i < final_value; i++){
...
}
enhanced for loop
for(final elem in Iterable_type){
...
}
while loop
while(condition){
...
}
do{
...
}while(condition)
break
continue
Basic Class Structure
class Rectangle{
//fields
double? width, length;
//Constructors
Rectangle(double w, double l){
width = w;
length = l;
}
/*Simplified version of Constructor
Rectangle(this.width,this.length);
*/
//methods
double getArea(){
return width * length;
}
/*Simplified version of the method
double GetArea() => width*length;
*/
}