Dart notes:
1- using final and const with data collection
you cannot modify data collection "Set,List,and Map' if it is const .
example:
const List empList = ['Tarek','Ali','Hamed'];
empList.add('Adel'); // Error in correct
empList = ['Hani']; // error In correct
However , with final there is a difference. You can modify a final List ,Set,Map using only add or addAll method.
example :
final List empList = ['Tarek','Ali','Hamed'];
empList.add('Hani'); // correct
empList.addAll(list2); //correct
2- Switch case
used to test literal value.
example:
String name = 'Ramy';
Switch(name) {
case 'rami': print(false);
case 'RamY': print(false);
case 'Ramy' : print(true);
default:
print('not valid');
}
In dart, you do not have to use break, it will break automatically.Unlike java, if condition evaluated true,statment will be executed and execution will be exited.
3- handling exceptions
In dart, syntax is different.
example:
try{
// code
on(NoSuchMethodError){
// handle the exception
}
catch(err){
print($err);
}
}
4-functions
use [] to mark parameters as optional. example:
void add([int n1,int n2,int n3,int n4]){
////code
}
////code
}
use {} to make named parameter and optional too. example
void add({int n1, int n2, int n3}){
// code
}
// code
}
arrow function
the sign => means return.
example:
int add(int num1, int num2)=> num1+num2;
5-Constructors
To declare named constructor , use dot operator. example
Device.laptop(int ram, num size , String model , String color){}
6- Access Modifiers
Do not write public or private word. they are not key words in dart.
All attributes and method within a class are public by default.
To make attribute private use undescore before the name. example.
class CustomerAccount{
String customerName;
String? nationalId;
String? _creditCardNumber; // this is declared private
}
Comments