groupBy<K, V> function Null safety

Map<K, List<V>> groupBy<K, V>(
  1. List<V> values,
  2. K mappedBy(
    1. V item
    )
)

Implementation

Map<K, List<V>> groupBy<K, V>(List<V> values, K mappedBy(V item)) {
  final result = <K, List<V>>{};
  values.forEach((v) {
    final key = mappedBy(v);
    if (!result.containsKey(key)) {
      result[key] = <V>[];
    }
    result[key]!.add(v);
  });
  return result;
}