← Dart tutorial

Dart

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).

Example
void main() {
  var numbers = [1, 2, 3, 4, 5];
  var doubled = numbers.map((n) => n * 2).toList();
  print(doubled);
}
Output
[2, 4, 6, 8, 10]
Ready to go further?

These free tutorials are a preview of what a Code Secure Academy cohort covers, project work and all.

Explore our tracks
Don't miss out

Ready to build something real?

Create a free account to enroll in a track, and get notified the moment a cohort opens.

Create free account