mapRemoveValue<K, V> static method

void mapRemoveValue<K, V>(
  1. Map<K, List<V>> map,
  2. K key,
  3. V value
)

Removes a value from a map of lists.

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

Performance vs Safety Tradeoff:

  • Safety: No parameter mutation, predictable behavior, safer for concurrent code
  • Cost: Allocates a new list on each call (filters existing list)
  • When it matters: Large lists (>1000 items) with frequent operations
  • When it doesn't: Typical use cases with small to medium lists

Note: Removes all occurrences of the value, not just the first one.

Example:

final map = <String, List<int>>{'scores': [100, 200, 100]};
MapUtils.mapRemoveValue(map, 'scores', 100); // Creates new list [200]

Implementation

static void mapRemoveValue<K, V>(Map<K, List<V>> map, K key, V value) {
  if (value == null) return;
  // ignore: avoid_parameter_mutation - Function is designed to mutate the map parameter
  map.update(
    key,
    (List<V> list) => list.where((V v) => v != value).toList(),
    ifAbsent: () => <V>[],
  );
}