The syntax of Map.removeWhere() method is:
void removeWhere(bool test(K key, V value))
This removeWhere() method of Map removes all entries of this map that satisfy the given test
.
Parameter | Optional/Required | Description |
---|---|---|
test | required | the function that determines if an entry should be removed |
Map.removeWhere() returns value of type void
.
In this example,
map
with some key-value pairs.removeWhere()
method with a function that removes entries with even values.map
is printed, showing the remaining entries.void main() {
Map<String, int> map = {'one': 1, 'two': 2, 'three': 3};
map.removeWhere((key, value) => value % 2 == 0);
print(map); // Output: {one: 1, three: 3}
}
{one: 1, three: 3}
In this example,
map
with some key-value pairs.removeWhere()
method with a function that removes entries with values of length 3.map
is printed, showing the remaining entries.void main() {
Map<int, String> map = {1: 'One', 2: 'Two', 3: 'Three'};
map.removeWhere((key, value) => value.length == 3);
print(map); // Output: {1: One}
}
{3: Three}
In this example,
map
with some key-value pairs.removeWhere()
method with a function that removes entries with keys starting with 't'.map
is printed, showing the remaining entries.void main() {
Map<String, String> map = {'fruit': 'apple', 'color': 'red', 'taste': 'sweet'};
map.removeWhere((key, value) => key.startsWith('t'));
print(map); // Output: {fruit: apple, color: red}
}
{fruit: apple, color: red}
In this Dart tutorial, we learned about removeWhere() method of Map: the syntax and few working examples with output and detailed explanation for each example.