Type | mutable? | ordered? |
---|---|---|
List | mutable | ordered |
Set | mutable | not ordered |
Map | mutable | not ordered |
String / Int | immutable |
var planets = ['Mercury', 'Venus', 'Earth', 'Mars'];
print(planets[0]); // Mercury
planets[0] = 'Saturn';
print(planets[0]); // Saturn
planets.removeAt(0);
Creating an empty, growable list
var list = List();
list.add('Mercury');
list.add('Venus');
Null safe list
var nullSafeList = List.empty(growable: true);
List with fixed length
var list = List.filled(3, 0);
Creating list with item of same types
var intList = List<Int>.filled(3, 1);
Iterating a list
var iterator = planets.iterator;
while(iterator.moveNext()){
print(iterator.current);
}
var planetsMap = {1 : "Mercury", 2 : "Venus", 3 : "Earth", 4 : "Mars"};
planetsMap[5] = "Jupitor";
Adding a collection
planetsMap.addAll({6 : "Saturn", 7 : "Uranus", 8 : "Neptune"});
Adding if key does not exists
planetsMap.putIfAbsent(1, () => "Mercury");
Removing
planetsMap.remove(1);
planetsMap.removeWhere((key, value) => value.startsWith('M'));
Map with specific data type
Map<int, String> planetMaps = {1 : 'Mercury', 2 : 'Venus', 3 : 'Earth'};
Iterating a map
for(int i in planetsMapat.keys){
print(planetsMap[i]);
}
Set hashSet = HashSet(); // no specified order
Set linkedHashSet = LinkedHasSet(); // insertation based on the order the items
var splayTreeSet = SplayTreeSet(); // store data that is comparable, no null values permitted
Adding an item
var set = <int> {1, 2, 3, 4, 5};
set.add(6);
print(set);
Updating the set
var newSet = set.map((e) => e == 1 ? 11 : e).toSet();
Removing an item
set.remove(5)
Iterating on a set
set.forEach((element) {
print(element);
});
'${5 + 7}' // 12
'${"hello".toUpperCase()}' // HELLO
'$5' // 5.toString()
int i = null; // invalid
int? i = null; // valid
int? i; // value is null initially
i ??= 5; // value updates to 5
i ??= 7; // value is still 5
printEmployee(String name, {required String department, required bool isActive}) {
}
// positional arg name
// required arg department and isActive
printEmployee("", isActive: false, department: "IT");
Functionn lambdaFunction = {int number, String name} {
};
var lambdaFunction2 = {double number, String name} {
};
Function arrowFunction = (int a, int b) => a + b;
Function arrowFunction2 = (int a, int b) => a + b;
// same as above with different syntax
Function arrowFunction2 = (int a, int b){
return a + b;
};