mapAddValue<K, V> static method

void mapAddValue<K, V>({
  1. required Map<K, List<V>> map,
  2. required K key,
  3. required V value,
})

Adds value to the list at key within map, creating the list if absent.

Uses an immutable approach: creates a new list rather than mutating the existing one.

Example:

final map = <String, List<int>>{};
MapExtensions.mapAddValue(map: map, key: 'scores', value: 100); // {'scores': [100]}
MapExtensions.mapAddValue(map: map, key: 'scores', value: 200); // {'scores': [100, 200]}

Implementation

static void mapAddValue<K, V>({required Map<K, List<V>> map, required K key, required V value}) {
  if (value == null) return;
  // ignore: saropa_lints/avoid_parameter_mutation -- function is designed to mutate the map parameter
  map.update(key, (List<V> list) => [...list, value], ifAbsent: () => <V>[value]);
}