The syntax of List.remove() method is:
bool remove(Object? value)
This remove() method of List removes the first occurrence of value
from this list.
Parameter | Optional/Required | Description |
---|---|---|
value | required | the value to remove from the list |
List.remove() returns value of type bool
.
In this example,
numbers
containing integers.remove()
method to remove the value 3
from numbers
.remove()
method returns true
if the value was found and removed, false
otherwise.void main() {
List<int> numbers = [1, 2, 3, 4, 5];
bool removed = numbers.remove(3);
print('Removed: $removed, List after removal: $numbers');
}
Removed: true, List after removal: [1, 2, 4, 5]
In this example,
characters
containing characters.remove()
method to remove the value 'c'
from characters
.remove()
method returns true
if the value was found and removed, false
otherwise.void main() {
List<String> characters = ['a', 'b', 'c', 'd'];
bool removed = characters.remove('c');
print('Removed: $removed, List after removal: $characters');
}
Removed: true, List after removal: [a, b, d]
In this example,
fruits
containing strings.remove()
method to remove the value 'apple'
from fruits
.remove()
method returns true
if the value was found and removed, false
otherwise.void main() {
List<String> fruits = ['apple', 'banana', 'cherry', 'apple'];
bool removed = fruits.remove('apple');
print('Removed: $removed, List after removal: $fruits');
}
Removed: true, List after removal: [banana, cherry, apple]
In this Dart tutorial, we learned about remove() method of List: the syntax and few working examples with output and detailed explanation for each example.