How to clear a list in Dart?

Viewed 5222

There are two options over there to clear some list

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

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

2 Answers

.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

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.

Related