mergeMaps<K, V> function

Map<K, V> mergeMaps <K, V>(Map<K, V> map1, Map<K, V> map2, { V value(V value1, V value2) })

Returns a new map with all key/value pairs in both map1 and map2.

If there are keys that occur in both maps, the value function is used to select the value that goes into the resulting map based on the two original values. If value is omitted, the value from map2 is used.

Implementation

Map<K, V> mergeMaps<K, V>(Map<K, V> map1, Map<K, V> map2,
    {V value(V value1, V value2)}) {
  var result = new Map<K, V>.from(map1);
  if (value == null) return result..addAll(map2);

  map2.forEach((key, mapValue) {
    result[key] =
        result.containsKey(key) ? value(result[key], mapValue) : mapValue;
  });
  return result;
}