removeDuplicatesBy<T> static method

List<T> removeDuplicatesBy<T>({
  1. required List<T>? items,
  2. required dynamic by(
    1. T a
    ),
})

Returns a list of unique values identified by the given function parameter by. The order of all unique items are maintained.

If the given parameter is null an empty list will be returned.

Implementation

static List<T> removeDuplicatesBy<T>({
  required List<T>? items,
  required dynamic Function(T a) by,
}) {
  if (items == null) {
    return [];
  }
  Map<dynamic, T> uniqueValues = HashMap<dynamic, T>();
  for (var item in items) {
    if (!uniqueValues.containsKey(by(item))) {
      uniqueValues[by(item)] = item;
    }
  }
  return uniqueValues.values.toList();
}