mapAddValue<K, V> static method
Adds a value to 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
- When it matters: Large lists (>1000 items) with frequent operations
- When it doesn't: Typical use cases with small to medium lists
Example:
final map = <String, List<int>>{};
MapUtils.mapAddValue(map, 'scores', 100); // Creates ['scores': [100]]
MapUtils.mapAddValue(map, 'scores', 200); // Creates new list [100, 200]
Implementation
static void mapAddValue<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, value], ifAbsent: () => <V>[value]);
}