distinctBy<T> function Null safety

Iterable<T> distinctBy<T>(
  1. Iterable<T>? list,
  2. dynamic by(
    1. T input
    )
)

Implementation

Iterable<T> distinctBy<T>(Iterable<T>? list, dynamic by(T input)) {
  if (list == null) {
    return [];
  }
  final keys = <dynamic>{};
  return list.where((item) {
    final key = by(item);
    if (!keys.contains(key)) {
      keys.add(key);
      return true;
    } else {
      return false;
    }
  }).toList();
}