Map> groupBy(Iterable values, S Function(T) key) { var map = >{}; for (var element in values) { (map[key(element)] ??= []).add(element); } return map; } Map groupCount(Map> values) { var map = {}; for (var element in values.keys) { map["$element"] = values[element]?.length ?? 0; } return map; } Map groupItem(Map> values) { var map = {}; for (var element in values.keys) { if (values[element] == null) { continue; } map["$element"] = values[element].first; } return map; } extension ListExtension on Iterable { Map> lGroupBy(S Function(T) key) { return groupBy(this, key); } } extension MapExtension on Map> { Map get mGroupCount => groupCount(this); Map get mGroupItem => groupItem(this); }