groupBy<K, V> method

Map<K, List<V>> groupBy<K, V>(
  1. K keySelector(
    1. T element
    ), {
  2. V valueTransform(
    1. T element
    )?,
})

Groups the elements of the list into a map by a key that is defined by a keySelector function.

The optional valueTransform function can be used to remap your elements.

Example:

var map = [1, 2, 3, 97, 98, 99].groupBy((n) => n < 10 ? 'smallNumbers' : 'largeNumbers')
// map = {'smallNumbers': [1, 2, 3], 'largeNumbers': [97, 98, 99]}
List<Person> persons = [
    Person(name: 'John', age: 21),
    Person(name: 'Carl', age: 18),
    Person(name: 'Peter', age: 56),
    Person(name: 'Sarah', age: 61)
];
var map = persons.groupBy((p) => p.age < 40 ? 'young' : 'old',
       valueTransform: (p) => p.name);
// map = {'young': ['John', 'Carl'], 'old': ['Peter', 'Sarah']}

Implementation

Map<K, List<V>> groupBy<K, V>(K Function(T element) keySelector,
    {V Function(T element)? valueTransform}) {
  ArgumentError.checkNotNull(keySelector);

  valueTransform ??= (element) => element as V;

  final map = <K, List<V>>{};

  forEach((element) {
    final key = keySelector(element);

    if (!map.containsKey(key)) {
      map[key] = [];
    }
    map[key]!.add(valueTransform!(element));
  });

  return map;
}