Lists
A List holds an ordered collection of values:
var numbers = [1, 2, 3, 4, 5];.
Example: transforming a list with map()
void main() {
var numbers = [1, 2, 3, 4, 5];
var doubled = numbers.map((n) => n * 2).toList();
print(doubled);
}
[2, 4, 6, 8, 10]
.map() transforms every item using the function you pass
it — here, the arrow function from the previous lesson — and returns a
new, lazily-evaluated sequence, which .toList() then
converts into a real, concrete list. This is the same underlying idea as
JavaScript's .map() array method, just Dart's particular
spelling of it (with that extra .toList() step Dart requires
that JavaScript doesn't).
void main() {
var numbers = [1, 2, 3, 4, 5];
var doubled = numbers.map((n) => n * 2).toList();
print(doubled);
}
[2, 4, 6, 8, 10]