rozerro
rozerro

Reputation: 7118

How to clear a list in Dart?

There are two options over there to clear some list

List<String> foo = ['a', 'b'];
...
foo = [];
// vs
foo.clear();

What is the best option? And when to use these variants?

Upvotes: 15

Views: 11659

Answers (2)

M Karimi
M Karimi

Reputation: 3485

var list_name = new List() 

--- creates a list of size zero

list_name = [val1,val2,val3]   

--- a list containing the specified values

 list_name = []   

--- clear elements with new reference

  list_name.clear()

--- removes every element from the list, but retains the list itself and it's type cast. --- it's best option to remove all elements of list.

Upvotes: 7

Nooruddin Lakhani
Nooruddin Lakhani

Reputation: 6967

.clear() will remove data from list with the same reference and foo = [] will have clear data with new reference

Note: .clear() is the best option

Upvotes: 20

Related Questions