Map addAll()

Map addAll() method adds all the key-value pairs of the given Map to this Map.

Syntax

The syntax to add all the key-value pairs of the Map map2 to Map map1 is

</>
Copy
map1.addAll(map2);

The method returns void.

Examples

Add all elements of one Map to other Map

In the following Dart Program, we take two Maps: map1 and map2, and add all the key-value pairs of map2 to map1, using Map.addAll() method.

main.dart

</>
Copy
void main() {
    var map1 = {'apple': 25, 'banana': 10};
    var map2 = {'cherry': 6, 'mango': 10};
    map1.addAll(map2);
    print(map1);
}

Output

{apple: 25, banana: 10, cherry: 6, mango: 10}

Conclusion

In this Dart Tutorial, we learned how to add all the key-value pairs of another Map to this Map, using Map.addAll() method.