Dart List Methods
List Methods and Properties
Dart lists have many built-in methods and properties.
This table shows the most common ones:
| Method or property | Description |
|---|---|
add() | Adds an item to the end of the list |
addAll() | Adds all items of another collection to the end of the list |
any() | Returns true if at least one item passes a test |
clear() | Removes all items from the list |
contains() | Returns true if the list has the given value |
every() | Returns true if all items pass a test |
first | The first item of the list |
fold() | Combines all items into one value, starting from a value you give |
forEach() | Runs a function for each item |
indexOf() | Returns the index of the first matching item, or -1 |
insert() | Adds an item at a given index |
isEmpty | True if the list has no items |
isNotEmpty | True if the list has at least one item |
join() | Joins all items into one string |
last | The last item of the list |
length | The number of items in the list |
map() | Creates new values by running a function on each item |
reduce() | Combines all items into one value, starting from the first item |
remove() | Removes the first item with the given value |
removeAt() | Removes the item at a given index |
removeLast() | Removes the last item |
removeWhere() | Removes all items that pass a test |
reversed | The items in reverse order |
sort() | Sorts the list |
sublist() | Returns a new list with a part of the items |
toList() | Creates a list from the items |
where() | Keeps only the items that pass a test |
Sort a List
The sort() method sorts the list in place.
Numbers are sorted from low to high, and strings are sorted alphabetically:
Example
void main() {
final numbers = [42, 7, 19, 3];
numbers.sort();
print(numbers);
final fruits = ['cherry', 'apple', 'banana'];
fruits.sort();
print(fruits);
}
Try it Yourself »
Result:
[3, 7, 19, 42]
[apple, banana, cherry]
To sort in another order, give sort() a function that compares two items, a and b.
The compareTo() method returns a negative number, zero or a positive number, which tells sort() which item comes first.
Here we compare b with a, to sort from high to low:
Example
void main() {
final scores = [72, 95, 58, 81];
scores.sort((a, b) => b.compareTo(a));
print(scores);
}
Try it Yourself »
Result:
[95, 81, 72, 58]
map() and where()
The map() method runs a function on each item and gives the new values.
The where() method keeps only the items that pass a test.
Both methods return an Iterable, a collection that you can loop through.
Use toList() to turn the result into a list:
Example
void main() {
final prices = [10, 25, 40, 5];
final doubled = prices.map((price) => price * 2).toList();
print(doubled);
final cheap = prices.where((price) => price < 20).toList();
print(cheap);
}
Try it Yourself »
Result:
[20, 50, 80, 10]
[10, 5]
The original list does not change.
reduce() and fold()
The reduce() and fold() methods combine all the items into one value.
reduce() starts with the first item, and then combines it with each next item:
Example
void main() {
final numbers = [3, 8, 2, 5];
final sum = numbers.reduce((a, b) => a + b);
print(sum);
}
Try it Yourself »
Result:
18
fold() starts with a value that you give.
Use fold() when the list can be empty, or when the result has another type than the items:
Example
void main() {
final words = ['Dart', 'is', 'fun'];
final letters = words.fold(0, (total, word) => total + word.length);
print(letters);
}
Try it Yourself »
Result:
9
On an empty list, reduce() throws an error, because there is no first item:
Example
void main() {
final numbers = <int>[];
final total = numbers.fold(0, (a, b) => a + b);
print(total);
print(numbers.reduce((a, b) => a + b));
}
Try it Yourself »
Result:
0
Unhandled exception:
Bad state: No element
The fold() call works, and prints its start value 0.
join() and reversed
The join() method joins all items into one string, with the separator you give.
The reversed property gives the items in reverse order:
Example
void main() {
final fruits = ['apple', 'banana', 'cherry'];
print(fruits.join(', '));
print(fruits.join(' and '));
print(fruits.reversed.toList());
}
Try it Yourself »
Result:
apple, banana, cherry
apple and banana and cherry
[cherry, banana, apple]
any() and every()
The any() method returns true if at least one item passes the test.
The every() method returns true only if all items pass the test:
Example
void main() {
final ages = [17, 22, 35, 41];
print(ages.any((age) => age < 18));
print(ages.every((age) => age >= 18));
}
Try it Yourself »
Result:
true
false