Merging Two Maps in Dart
How to merge two map in flutter

Hi Guy’s In this Tutorial, Let’s checkout how to merge 2 maps into one in flutter dart.

If you are working with dart programming language you might have come with situation when you want to combine 2 maps objects. For Example let’s say, a Map that stores name of a user, his phone number and email address and there is another map that contain that user house address, his family details etc. In such scenerio merging 2 maps in single map is benefit because all the information about user is one one data itself.

In this dart article, let’s walk through differents ways of join 2 maps in one.

1. Using (…) spread operator

2. Using addAll() method.

3. Using putIfAbsent() method.


Merging 2 Maps Object using spread operator

This is one way by which we can easily join/combine 2 map in one i.e using spread operator (…). By using thi approach two collection of maps are merged into one collection.

Example:

// main.dart

void main() {
  Map<String, int> map1 = {'a': 1, 'b': 2};
  Map<String, int> map2 = {'c': 3, 'd': 4, 'c': 5};

  Map<String, int> mergedMap = {...map1, ...map2};

  print(mergedMap);
}

Output:

{a: 1, b: 2, c: 5, d: 4}

Note: Here if there are duplicate keys present in the maps that you are merging then in such case the value of second map will be overwriten the value of first map. In above example as you can see key c value is 3 and same in map2 key c value is 5 so final result we get is c:5 that is from map2.


Merge 2 maps using addAll() method

Their is another way by which we can merge map in dart that is by using addAll() method, What this function does is it simply copy all the key & value from map 2 to map 1. let’s understand with an example

Example:

void main() {
  Map<String, int> map1 = {'a': 1, 'b': 2,'e':1};
  Map<String, int> map2 = {'c': 3, 'd': 4, 'e': 5};

  // merge map2 into map1
  map1.addAll(map2);

  print(map1);
 
}

Output:

{a: 1, b: 2, e: 5, c: 3, d: 4}

addAll() method copy all the key & value from map 2 and add it into map 1, That means addAll() method will directly modify the first map.


Merge 2 map using putifAbsent() method

As we saw in above ways, The value of a key is modified if same key is present. To solve this problem we can make use of putifAbsent() method. Let’s understand with an example

// main.dart
void main() {
  Map<String, String> map1 = {'firstname': "Rajat", 'lastname': "Palankar"};
  Map<String, String> map2 = {'address': "India", 'firstname': "NameChanged", 'website': "protocoderspoint.com"};

  map2.forEach((key, value) {
    map1.putIfAbsent(key, () => value);
  });
    print(map1);
}

Output:

{firstname: Rajat, lastname: Palankar, address: India, website: protocoderspoint.com}

This approach to join or merge 2 map is useful then you don’t want to keep duplicate data. In above example I am iterating map2 using forEach loop then checking is same key present in map 1, if not then add map 2 key and value in map 1 using putIfAbsent() and ignore it.