distinctBy method

List<T> distinctBy(
  1. Object? selector(
    1. T e
    )
)

Returns a list of unique elements based on a selector function.

Example:

var list = [{'id': 1}, {'id': 1}, {'id': 2}];
var result = list.distinctBy((e) => e['id']); // [{'id': 1}, {'id': 2}]

Implementation

List<T> distinctBy(Object? Function(T e) selector) {
  final set = <Object?>{};
  return where((e) => set.add(selector(e))).toList();
}