How to remove items from a List in Flutter

October 16, 2022 No comments flutter dart list remove

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:

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.

{{ message }}

{{ 'Comments are closed.' | trans }}