This tutorial is about how to remove elements from List
using various built-in methods.
For this tutorial, we are going to use the following List
.
List<String> strings = ['one', 'two', 'three', 'four', 'five'];
Using .remove(Object value)
This is for removing the first occurrence of an element with the given value.
strings.remove('one');
print(strings);
Output:
[two, three, four, five]
If there are multiple elements with value 'one', only the first one will be removed. If you need to remove multiple matching elements, use .removeWhere()
instead.
For List
of objects, matching is done by the object instance. For example, there is a List
of Item
objects.
class Person {
int id;
String name;
Person({this.id, this.name});
}
There are two elements with the same value: person1
and person3. Then, we try to remove person3
.
Person person1 = new Person(id: 1, name: "Name one");
Person person2 = new Person(id: 2, name: "Name two");
Person person3 = new Person(id: 1, name: "Name one");
List people = [person1, person2, person3];
people.remove(person3);
people.forEach((Person person) {
print('${person.id} - ${person.name}');
});
Though person1
has the same values as person3
and it appears first, person3
is the one that will be removed.
Using .removeAt(int index)
You can use this method if you know the index of the item to be removed. The index starts from 0.
strings.removeAt(1);
print(strings);
Output:
[one, three, four, five]
If you try to delete index not in range, here's the error you will get.
Unhandled exception:
RangeError (index): Invalid value: Not in range 0..4, inclusive: 5
Using .removeLast()
For removing the last element in a List
, use .removeLast()
.
strings.removeLast();
print(strings);
Output:
[one, two, three, four]
If you apply .removeLast()
on empty List
, you'll get the following error:
Unhandled exception:
RangeError (index): Invalid value: Valid value range is empty: -1
If you are not sure whether the List
is empty, you can use .isNotEmpty
before calling .removeLast()
.
strings.isNotEmpty ?? strings.removeLast();
Using .removeRange(int start, int end)
It is used to remove elements in a range. The start
index is inclusive, while the end
index is exclusive. The following example remove elements starting at index 1 until element before index 3.
strings.removeRange(1, 3);
print(strings);
Output:
[one, four, five]
What happens if the start or end index is invalid. Here are the rules
- If
start
<end
, remove elements from indexstart
until index end - 1. - If
start
=end
, no element will be removed. - If the range is invalid (either
start
/end
index is out of range, orstart
>end
, it will throwRangeError
exception.
Using .removeWhere(bool Function test)
With this method, you can remove multiple elements that match the criteria. The passed Function
must have one argument and return boolean
. true
means the item should be removed. The below examples removes element whose length is 3.
strings.removeWhere((item) => item.length == 3);
print(strings);
Output:
[three, four, five]
If there is no element matching the criteria, nothing will be removed and no error will be thrown.
That's how to remove elements from a Dart List
.