List Use the array-like Dart List to manage a collection of objects

This lesson is available for PRO members or as a single course purchase. Sign-in and choose a plan below.
Get Unlimited PRO Access

OR


*Enrollment provides full access to this course (and updates) for life.

Basic Lists

file_type_dartlang lists.dart
List<int> list = [1, 2, 3, 4, 5];

list[0]; // 1

list.length; // 5
list.last; // 5
list.first; // 1

Loops

file_type_dartlang lists.dart
  for (int n in list) {
    print(n);
  }

list.forEach((n) => print(n));

var doubled = list.map((n) => n * 2);

Spread Syntax

file_type_dartlang lists.dart
var combined = [...list, ...doubled];
combined.forEach(print);

Conditions in Lists

file_type_dartlang lists.dart
bool depressed = false;
var cart = [
    'milk', 
    'eggs', 
    if (depressed) 'Vodka'
];

Q&A Chat