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

ParameterOptional/RequiredDescription
updaterequiredA 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,

  1. We create a map named map with initial key-value pairs.
  2. We then use the updateAll() method on map, passing a function that doubles each value.
  3. After the update operation, the values in map are doubled.
  4. 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,

  1. We create a map named map with initial key-value pairs.
  2. We then use the updateAll() method on map, passing a function that converts each value to uppercase.
  3. After the update operation, all values in map are in uppercase.
  4. 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,

  1. We create a map named map with initial key-value pairs.
  2. We then use the updateAll() method on map, passing a function that adds a prefix 'delicious' to each value.
  3. After the update operation, 'delicious' prefix is added to all values in map.
  4. 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.