Collection Literals
Spread
...
...other flattens another iterable into the literal in place. Useful for prepending or appending.
Program
Play the program to assemble a list from a head, tail, and bookend numbers.
spread.dart
Replay: real traced execution (multi-file project)
void main() {
var head = [1, 2];
var tail = [3, 4];
var all = [0, ...head, ...tail, 5];
print(all);
}
head ← [1, 2]
1void main() {2 var head = [1, 2];3 var tail = [3, 4];values this step[1, 2]headtail ← [3, 4]
2var head = [1, 2];3var tail = [3, 4];4var all = [0, ...head, ...tail, 5];values this step[3, 4]tailall ← [0, 1, 2, 3, 4, 5]
3var tail = [3, 4];4var all = [0, ...head, ...tail, 5];5print(all);values this step[0, 1, 2, 3, 4, 5]all[1, 2]head[3, 4]tailprint(all);
4 var all = [0, ...head, ...tail, 5];5 print(all);6}output[0, 1, 2, 3, 4, 5]values this step[0, 1, 2, 3, 4, 5]all
spread
`...xs` flattens `xs` into the surrounding literal.
null-aware spread
Use `...?xs` to skip the spread when `xs` is null.
composition
Combine literal elements with spread elements freely.