mergeListenable method

ListenableMap<K, V> mergeListenable(
  1. ValueListenable<Map<K, V>> others, {
  2. K convertKeys(
    1. K key
    )?,
  3. V convertValues(
    1. V value
    )?,
})

Merges the map in others with the current map.

If the same key is found, the value of others has priority.

Implementation

ListenableMap<K, V> mergeListenable(
  ValueListenable<Map<K, V>> others, {
  K Function(K key)? convertKeys,
  V Function(V value)? convertValues,
}) {
  final filter = (Map<K, V> origin) {
    final res = <K, V>{};
    for (final tmp in value.entries) {
      res[tmp.key] = tmp.value;
    }
    for (final tmp in others.value.entries) {
      final key = convertKeys?.call(tmp.key) ?? tmp.key;
      final value = convertValues?.call(tmp.value) ?? tmp.value;
      res[key] = value;
    }
    return res;
  };
  return ListenableMap<K, V>.from(filter(const {}))
    ..dependOn(this, filter)
    ..dependOn(others, filter);
}