Introduction
In Flutter/Dart, we may need to remove items from a List. We'll examine how to do it in this tutorial.
Other useful tips related to operations of lists in Flutter/Dart:
- Filter items in a List in Flutter
- Sort a List in Flutter
- FlatMap a list of lists in Flutter
- Reverse a List in Flutter
- Iterate over List in Flutter
- Find elements in List in Flutter
- Update a list item in Flutter
- Access items from List in Flutter
- Create a list in Flutter
- Combine Lists in Flutter
How to remove items from the List in Flutter
To remove items from a list in Flutter/Dart we can use methods such as: removeAt()
, remove()
, removeWhere()
, removeRange()
or clear()
.
Remove item at a specific index in List using removeAt()
method
var list = ['zero', 1, 2, 3];
list.removeAt(2);
print(list);
The output:
[zero, 1, 3]
Remove an item from a List by using the remove()
method
var list = ['zero', 1, 2, 3];
list.remove('zero');
print(list);
The output:
[1, 2, 3]
Remove all items that match specific condition using removeWhere()
method
Remove all items whose length is greater then 3:
var list = ['zero', 'one', 'two', 'three'];
list.removeWhere((item) => item.toString().length > 3);
print(list);
The output:
[one, two]
Remove all items in the range of a list by using the removeRange()
method
Remove items from index 1 to 3:
var list = ['zero', 1, 2, 3];
list.removeRange(1, 4);
print(list);
The output:
[zero]
Clear a list using clear()
method
var list = ['zero', 1, 2, 3];
list.clear();
print(list);
The output:
[]
Conclusion
In this article, we presented how several ways to remove items from a list in Flutter/Dart.
{{ 'Comments (%count%)' | trans {count:count} }}
{{ 'Comments are closed.' | trans }}