You are currently viewing How to clear a list in flutter?

How to clear a list in flutter?

In flutter or dart there can be several occasions where we need to clear or empty the list. It’s very easy to do this and this way flutter recommends as well.

Clear Method

The clear method, removes all objects from this list and the length of the list becomes zero.

Examle:

final numbers = <int>[1, 2, 3]; 
numbers.clear();
print(numbers.length); // 0
print(numbers); // []

final List<Map<String, dynamic>> users = [{"id":1, "name":"Joan"}, {"id":2, "name":"Peter"}];
users.clear();
print(users.length); // 0
print(users); // []

It’s best option to remove all elements of list. With clearing the list it keeps the list with it’s type cast. For more details about this you can visit the flutter official document of clear method.

 

This Post Has One Comment

  1. Polish News

    Reading this piece felt like walking through a beautiful garden of ideas — each thought more inviting than the last.

Leave a Reply