The Map.map()
method can be used to create a new map with all entries modified or transformed.
map_name.map((k, v))
map_name
is the name of the map.
The map()
method requires a key-value pair of a specific map to transform to a new map.
Map.map()
returns a new key-value pair.
The following code shows how to use the Map.map()
method in Dart:
void main() {// Initialize mapMap map1 = {'one': 2, 'two': 6,'three': 9};//Use .map() methodvar transformedMap = map1.map((k, v) {return MapEntry(k.toUpperCase(), v*v);});print(map1);print('The transformed map: ${transformedMap}');}
The code above changes keys and values of all entries of map1
based on the condition to create the transformedMap
. This changes the keys to uppercase and multiplies the values by themselves.