deepMerge method

Map<K, V> deepMerge(
  1. Map<K, V> other
)

Recursively merges other into this map. For duplicate keys, if both values are Maps they are merged recursively; otherwise other's value wins.

{'a': {'x': 1}}.deepMerge({'a': {'y': 2}}) // {'a': {'x': 1, 'y': 2}}

Implementation

Map<K, V> deepMerge(Map<K, V> other) {
  final result = Map<K, V>.from(this);
  for (final e in other.entries) {
    if (result.containsKey(e.key) && result[e.key] is Map && e.value is Map) {
      result[e.key] = (result[e.key] as Map).deepMerge(e.value as Map) as V;
    } else {
      result[e.key] = e.value;
    }
  }
  return result;
}