The syntax of Map.addAll() method is:
void addAll(Map<K, V> other)
This addAll() method of Map adds all key/value pairs of other
to this map.
Parameter | Optional/Required | Description |
---|---|---|
other | required | the map whose key/value pairs will be added to this map |
Map.addAll() returns value of type void
.
In this example,
map1
and map2
, with key/value pairs.addAll()
method on map1
, passing map2
as the argument, to add all key/value pairs from map2
to map1
.map1
to standard output.void main() {
Map<String, int> map1 = {'apple': 1, 'banana': 2};
Map<String, int> map2 = {'cherry': 3};
map1.addAll(map2);
print('Updated map1: $map1');
}
Updated map1: {apple: 1, banana: 2, cherry: 3}
In this example,
map1
and map2
, with key/value pairs.addAll()
method on map1
, passing map2
as the argument, to add all key/value pairs from map2
to map1
.map1
to standard output.void main() {
Map<int, String> map1 = {1: 'one', 2: 'two'};
Map<int, String> map2 = {3: 'three'};
map1.addAll(map2);
print('Updated map1: $map1');
}
Updated map1: {1: one, 2: two, 3: three}
In this example,
map1
and map2
, with key/value pairs.addAll()
method on map1
, passing map2
as the argument, to add all key/value pairs from map2
to map1
.map1
to standard output.void main() {
Map<String, String> map1 = {'name': 'John'};
Map<String, String> map2 = {'age': '30'};
map1.addAll(map2);
print('Updated map1: $map1');
}
Updated map1: {name: John, age: 30}
In this Dart tutorial, we learned about addAll() method of Map: the syntax and few working examples with output and detailed explanation for each example.