The syntax of List.removeWhere() method is:
void removeWhere(bool test(E element))
This removeWhere() method of List removes all objects from this list that satisfy test
.
Parameter | Optional/Required | Description |
---|---|---|
test | required | the predicate function to test each element |
List.removeWhere() returns value of type void
.
In this example,
numbers
.removeWhere()
method on numbers
with a predicate function that removes elements divisible by 2 (i.e., even numbers).void main() {
List<int> numbers = [1, 2, 3, 4, 5];
numbers.removeWhere((element) => element % 2 == 0);
print(numbers);
}
[1, 3, 5]
In this example,
fruits
containing fruit names.removeWhere()
method on fruits
with a predicate function that removes fruits with a length greater than 5 characters.void main() {
List<String> fruits = ['apple', 'banana', 'cherry', 'orange'];
fruits.removeWhere((fruit) => fruit.length > 5);
print(fruits);
}
[apple]
In this example,
prices
representing item prices.removeWhere()
method on prices
with a predicate function that removes prices less than 10.void main() {
List<double> prices = [9.99, 15.49, 4.75, 23.99, 8.25];
prices.removeWhere((price) => price < 10);
print(prices);
}
[15.49, 23.99]
In this Dart tutorial, we learned about removeWhere() method of List: the syntax and few working examples with output and detailed explanation for each example.