Dart Lists
Dart Lists
A list stores many values in one variable.
The values in a list are called items, or elements.
To create a list, write the items in square brackets [], separated by commas:
Example
void main() {
final fruits = ['apple', 'banana', 'cherry'];
print(fruits);
}
Try it Yourself »
Result:
[apple, banana, cherry]
A list keeps its items in order, and it can have the same value more than once:
Result:
[10, 20, 10, 30]
Note: Many other languages call this an array.
In Dart, it is called a list.
Typed Lists
Every list has a type for its items.
In the examples above, Dart finds the type by itself from the values.
You can also write the type yourself, with List<Type>:
Example
void main() {
List<String> cars = ['Volvo', 'BMW', 'Ford'];
List<int> ages = [25, 31, 47];
print(cars);
print(ages);
}
Try it Yourself »
Result:
[Volvo, BMW, Ford]
[25, 31, 47]
A list of type List<String> can only hold strings.
If you try to add a number, the program does not run:
Example
void main() {
List<String> cars = ['Volvo', 'BMW', 'Ford'];
cars.add(42);
print(cars);
}
Try it Yourself »
Result:
main.dart:3:12: Error: The argument type 'int' can't be assigned to the parameter type 'String'.
cars.add(42);
^
To create an empty list, write the type before the brackets:
Example
void main() {
final names = <String>[];
print(names);
names.add('Liam');
print(names);
}
Try it Yourself »
Result:
[]
[Liam]
The add() method puts a new item at the end of the list.
You will learn more about it in the Change List Items chapter.
List Length
The length property gives the number of items in a list.
The isEmpty and isNotEmpty properties tell if the list has any items:
Example
void main() {
final fruits = ['apple', 'banana', 'cherry'];
final empty = <int>[];
print(fruits.length);
print(fruits.isEmpty);
print(fruits.isNotEmpty);
print(empty.length);
print(empty.isEmpty);
}
Try it Yourself »
Result:
3
false
true
0
true
Growable and Fixed-Length Lists
The lists you create with [] are growable.
This means you can add and remove items, so the length can change.
A fixed-length list always has the same number of items.
You can create one with List.filled(), which takes the length and a start value for every item:
Example
void main() {
final seats = List.filled(4, 'free');
print(seats);
seats[1] = 'taken';
print(seats);
}
Try it Yourself »
Result:
[free, free, free, free]
[free, taken, free, free]
You can change the items of a fixed-length list, but you cannot add or remove items.
If you try, Dart throws an error when the program runs:
Result:
Unhandled exception:
Unsupported operation: Cannot add to a fixed-length list
Constant and Unmodifiable Lists
A list created with const can never change.
You cannot add, remove or change its items:
Example
void main() {
const colors = ['red', 'green', 'blue'];
print(colors);
colors.add('yellow');
}
Try it Yourself »
Result:
[red, green, blue]
Unhandled exception:
Unsupported operation: Cannot add to an unmodifiable list
The value of a const list must be known when the program is compiled.
If the values are only known when the program runs, use List.unmodifiable() to create a copy that cannot change.
Remember: final and const do not mean the same thing for lists.
A final variable cannot point to a new list, but you can still change the items of the list.
A const list cannot change at all.
This example adds an item to a final list:
Example
void main() {
final fruits = ['apple', 'banana'];
fruits.add('cherry');
print(fruits);
}
Try it Yourself »
Result:
[apple, banana, cherry]