Dart – Remove all Elements of a List

To remove all Elements of a List in Dart, call clear() method on this list. clear() removes all the elements from this list and returns nothing, void.

Syntax

The syntax to remove all elements from a List list is

list.clear()
ADVERTISEMENT

Example

In this example, we take a list of integers with five elements, and remove all of the elements from this list using List.clear() method.

main.dart

void main() {
  var list = [2, 4, 8, 16, 32];
  print('Original List : $list');
  list.clear();
  print('Result List   : $list');
}

Output

Original List : [2, 4, 8, 16, 32]
Result List   : []

Conclusion

In this Dart Tutorial, we learned how to remove all elements from a list, with examples.