Map addEntries()

Map addEntries() method adds all the key-value pairs of the given Iterable<MapEntry> to this Map.

Syntax

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

</>
Copy
map1.addEntries(entries);

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. We get the iterator to entries in map2 using Map.entries property, and add all the key-value pairs of entries to map1, using Map.addEntries() method.

main.dart

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

Output

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

Conclusion

In this Dart Tutorial, we learned how to add all the entries (key-value pairs) given in an iterator, to this Map, using Map.addEntries() method.