mergeWith method

Map<K, V> mergeWith(
  1. Map<K, V> other, {
  2. V resolve(
    1. V existing,
    2. V incoming
    )?,
})

Merges this map with other. For duplicate keys, resolve determines the winner (defaults to other's value).

Implementation

Map<K, V> mergeWith(
  Map<K, V> other, {
  V Function(V existing, V incoming)? resolve,
}) {
  final result = Map<K, V>.from(this);
  for (final e in other.entries) {
    result[e.key] = (resolve != null && result.containsKey(e.key))
        ? resolve(result[e.key] as V, e.value)
        : e.value;
  }
  return result;
}