The syntax of Map.addEntries() method is:
void addEntries(Iterable<MapEntry<K, V>> newEntries)
This addEntries() method of Map adds all key/value pairs of newEntries
to this map.
Parameter | Optional/Required | Description |
---|---|---|
newEntries | required | an iterable of key/value pairs to add to the map |
Map.addEntries() returns value of type void
.
In this example,
map
with initial key/value pairs.newEntries
containing additional key/value pairs.addEntries()
method to add all key/value pairs from newEntries
to map
.map
after adding the entries to standard output.void main() {
var map = {'a': 1, 'b': 2};
var newEntries = {'c': 3, 'd': 4}.entries;
map.addEntries(newEntries);
print(map);
}
{a: 1, b: 2, c: 3, d: 4}
In this example,
map
with initial key/value pairs.newEntries
containing a single key/value pair.addEntries()
method to add the key/value pair from newEntries
to map
.map
after adding the entry to standard output.void main() {
var map = {'x': 'apple', 'y': 'banana'};
var newEntries = {'z': 'cherry'}.entries;
map.addEntries(newEntries);
print(map);
}
{x: apple, y: banana, z: cherry}
In this example,
map
with initial key/value pairs representing IDs and names.newEntries
containing a single key/value pair representing a new ID and name.addEntries()
method to add the new ID and name to map
.map
after adding the entry to standard output.void main() {
var map = {'id1': 'John', 'id2': 'Doe'};
var newEntries = {'id3': 'Jane'}.entries;
map.addEntries(newEntries);
print(map);
}
{id1: John, id2: Doe, id3: Jane}
In this Dart tutorial, we learned about addEntries() method of Map: the syntax and few working examples with output and detailed explanation for each example.