Skip to content

Latest commit

 

History

History
149 lines (137 loc) · 2.99 KB

dart_cheatsheet.md

File metadata and controls

149 lines (137 loc) · 2.99 KB

dart tips

Data Structures

Type mutable? ordered?
List mutable ordered
Set mutable not ordered
Map mutable not ordered
String / Int immutable

[List]

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);
}

[Map]

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]

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);
});

[String Interpolation]

'${5 + 7}' // 12
'${"hello".toUpperCase()}' // HELLO
'$5' // 5.toString()

[Nullable variables]

int i = null; // invalid
int? i = null; // valid
int? i; // value is null initially

[Null-aware operator]

i ??= 5; // value updates to 5
i ??= 7; // value is still 5

[Functions]

Null Safety

printEmployee(String name, {required String department, required bool isActive}) {

}
// positional arg name 
// required arg department and isActive
printEmployee("", isActive: false, department: "IT");

[Lambda function]

Functionn lambdaFunction = {int number, String name} {
};
var lambdaFunction2 = {double number, String name} {
};

[Arrow function]

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;
};