The syntax of List.clear() method is:
void clear()
This clear() method of List removes all objects from this list; the length of the list becomes zero.
List.clear() returns value of type void
.
In this example,
numbers
containing integers [1, 2, 3].clear()
method to remove all elements from the list.void main() {
List<int> numbers = [1, 2, 3];
numbers.clear();
print(numbers); // Output: []
}
[]
In this example,
characters
containing characters ['a', 'b', 'c'].clear()
method to remove all elements from the list.void main() {
List<String> characters = ['a', 'b', 'c'];
characters.clear();
print(characters); // Output: []
}
[]
In this example,
words
containing strings ['hello', 'world'].clear()
method to remove all elements from the list.void main() {
List<String> words = ['hello', 'world'];
words.clear();
print(words); // Output: []
}
[]
In this Dart tutorial, we learned about clear() method of List: the syntax and few working examples with output and detailed explanation for each example.