Dart Map updateAll()
Syntax & Examples
Syntax of Map.updateAll()
The syntax of Map.updateAll() method is:
void updateAll(V update(K key, V value))
This updateAll() method of Map updates all values.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
update | required | A function that updates each key-value pair. The function receives a key and its corresponding value as arguments. |
Return Type
Map.updateAll() returns value of type void
.
✐ Examples
1 Double each value in the map
In this example,
- We create a map named
map
with initial key-value pairs. - We then use the
updateAll()
method onmap
, passing a function that doubles each value. - After the update operation, the values in
map
are doubled. - We print the updated map to standard output.
Dart Program
void main() {
var map = {'a': 1, 'b': 2, 'c': 3};
map.updateAll((key, value) => value * 2);
print(map);
}
Output
{a: 2, b: 4, c: 6}
2 Convert all values to uppercase
In this example,
- We create a map named
map
with initial key-value pairs. - We then use the
updateAll()
method onmap
, passing a function that converts each value to uppercase. - After the update operation, all values in
map
are in uppercase. - We print the updated map to standard output.
Dart Program
void main() {
var map = {'x': 'A', 'y': 'B', 'z': 'C'};
map.updateAll((key, value) => value.toUpperCase());
print(map);
}
Output
{x: A, y: B, z: C}
3 Add prefix to all values in the map
In this example,
- We create a map named
map
with initial key-value pairs. - We then use the
updateAll()
method onmap
, passing a function that adds a prefix 'delicious' to each value. - After the update operation, 'delicious' prefix is added to all values in
map
. - We print the updated map to standard output.
Dart Program
void main() {
var map = {'fruit': 'apple', 'vegetable': 'carrot', 'grain': 'rice'};
map.updateAll((key, value) => 'delicious ' + value);
print(map);
}
Output
{fruit: delicious apple, vegetable: delicious carrot, grain: delicious rice}
Summary
In this Dart tutorial, we learned about updateAll() method of Map: the syntax and few working examples with output and detailed explanation for each example.