Lists

 A list can be created by

List<int> list = [1, 2, 3];
var list = [1, 2, 3];


spread operator (...)

var list;
var list2 = [0, ...?list];

if the list is null, need to use null-aware spread operator (...?)

var list;
var list2 = [0, ...list];

print(list2)//[ERROR:flutter/lib/ui/ui_dart_state.cc(166)] Unhandled Exception: NoSuchMethodError: The getter 'iterator' was called on null.  


for-in

var collection = [1, 2, 3];
for (var x in collection) {
print(x); // 1 2 3
}



Method

.length

var list = [1, 2, 3];
print(list.length); //3


.forEach()

  void printElement(int element) {
print(element);
}

var list = [1, 2, 3];

// Pass printElement as a parameter.
list.forEach(printElement);

.indexOf()

var list = ['apples', 'bananas', 'oranges'];
list.forEach((item) {
print('${list.indexOf(item)}: $item');
});
// 0: apples
// 1: bananas
// 2: oranges

arrow notation:

list.forEach(
(item) => print('${list.indexOf(item)}: $item'));

.add()

var callbacks = [];
for (var i = 0; i < 2; i++) {
callbacks.add(() => print(i));
}
callbacks.forEach((c) => c());


.where()

Returns a new lazy Iterable with all elements that satisfy the predicate test.

candidates
.where((c) => c.yearsExperience >= 5)
.forEach((c) => c.interview());




Comments