deepMerge method

  1. @useResult
Map<String, dynamic> deepMerge(
  1. Map<String, dynamic> other
)

Merges other into this map recursively. Values in other overwrite. Nested maps are merged; lists and other values are replaced.

Recurses to the maps' nesting depth; not intended for untrusted, arbitrarily-deep input (deep nesting can exhaust the stack).

Implementation

@useResult
Map<String, dynamic> deepMerge(Map<String, dynamic> other) {
  final Map<String, dynamic> result = Map<String, dynamic>.from(this);
  for (final MapEntry<String, dynamic> e in other.entries) {
    final dynamic existing = result[e.key];
    final dynamic otherVal = e.value;
    if (existing is Map<String, dynamic> && otherVal is Map<String, dynamic>) {
      result[e.key] = existing.deepMerge(otherVal);
    } else {
      result[e.key] = e.value;
    }
  }
  return result;
}