Dart List remove()
Syntax & Examples
Syntax of List.remove()
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.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
value | required | the value to remove from the list |
Return Type
List.remove() returns value of type bool
.
✐ Examples
1 Remove a number from the list
In this example,
- We create a list named
numbers
containing integers. - We use the
remove()
method to remove the value3
fromnumbers
. - The
remove()
method returnstrue
if the value was found and removed,false
otherwise. - We print the removal status and the list after removal to standard output.
Dart Program
void main() {
List<int> numbers = [1, 2, 3, 4, 5];
bool removed = numbers.remove(3);
print('Removed: $removed, List after removal: $numbers');
}
Output
Removed: true, List after removal: [1, 2, 4, 5]
2 Remove a character from the list
In this example,
- We create a list named
characters
containing characters. - We use the
remove()
method to remove the value'c'
fromcharacters
. - The
remove()
method returnstrue
if the value was found and removed,false
otherwise. - We print the removal status and the list after removal to standard output.
Dart Program
void main() {
List<String> characters = ['a', 'b', 'c', 'd'];
bool removed = characters.remove('c');
print('Removed: $removed, List after removal: $characters');
}
Output
Removed: true, List after removal: [a, b, d]
3 Remove a string from the list
In this example,
- We create a list named
fruits
containing strings. - We use the
remove()
method to remove the value'apple'
fromfruits
. - The
remove()
method returnstrue
if the value was found and removed,false
otherwise. - We print the removal status and the list after removal to standard output.
Dart Program
void main() {
List<String> fruits = ['apple', 'banana', 'cherry', 'apple'];
bool removed = fruits.remove('apple');
print('Removed: $removed, List after removal: $fruits');
}
Output
Removed: true, List after removal: [banana, cherry, apple]
Summary
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.