The syntax of Map.remove() method is:
V? remove(Object? key)
This remove() method of Map removes key
and its associated value, if present, from the map.
Parameter | Optional/Required | Description |
---|---|---|
key | required | the key to be removed from the map |
Map.remove() returns value of type V?
.
In this example,
map
with key-value pairs {'a': 1, 'b': 2, 'c': 3}.remove()
method.void main() {
var map = {'a': 1, 'b': 2, 'c': 3};
map.remove('b');
print(map);
}
{a: 1, c: 3}
In this example,
map
with key-value pairs {'x': 'apple', 'y': 'banana', 'z': 'cherry'}.remove()
method and store the removed value in removedValue
.void main() {
var map = {'x': 'apple', 'y': 'banana', 'z': 'cherry'};
var removedValue = map.remove('y');
print('Removed value: $removedValue');
}
Removed value: banana
In this example,
map
with key-value pairs {1: 'one', 2: 'two', 3: 'three'}.removedKey
as 2.remove()
method and store the removed value in removedValue
.void main() {
var map = {1: 'one', 2: 'two', 3: 'three'};
var removedKey = 2;
var removedValue = map.remove(removedKey);
print('Removed entry: ($removedKey, $removedValue)');
}
Removed entry: (2, two)
In this Dart tutorial, we learned about remove() method of Map: the syntax and few working examples with output and detailed explanation for each example.