removeDuplicates method

void removeDuplicates({
  1. dynamic by(
    1. T item
    )?,
  2. bool removeNulls = false,
})

Removes all duplicates from the list, leaving only the distinct items. Optionally, you can provide an id function to compare the items.

If you pass removeNulls as true, it will also remove the nulls (it will check the item is null, before applying the by function).

See also: distinct to return a new list and keep the original unchanged.

See also: whereNoDuplicates in FicIterableExtension for a lazy version.

Implementation

void removeDuplicates({
  dynamic Function(T item)? by,
  bool removeNulls = false,
}) {
  if (by != null) {
    Set<dynamic> ids = <dynamic>{};
    removeWhere((item) {
      if (removeNulls && item == null) return true;
      dynamic id = by(item);
      return !ids.add(id);
    });
  } else {
    Set<T> items = {};
    removeWhere((item) {
      if (removeNulls && item == null) return true;
      return !items.add(item);
    });
  }
}