distinctBy
Removes duplicates as determined by a key function, rather than value equality.
Lecture
distinctBy is uniq's general form: instead of
comparing whole elements, it runs each element through f
and keeps only the first element to produce each distinct key.
distinctBy is the Dart-idiomatic name; fxdart also accepts
the FxTS spelling uniqBy — they're the same operator. It's the
tool for "one row per customer," "one event per type," or dedupe by any
field or computed value — uniq itself is just
distinctBy((a) => a, iterable).
As with uniq, it's lazy and order-preserving: the first
element for each key wins, and the internal Set<B>
of seen keys only grows as you pull.
The same async rule applies here as with uniq: put the
concurrency in an upstream fetch (.map(...).concurrent(n)),
then apply distinctBy/distinctByAsync to the
already-resolved, in-order stream.
Demo 1 · Basics
Demo 2 · Async, with concurrency upstream
Try it yourself
Exercise: use distinctBy to keep only the first event of each
'type'.
uniq — dedupe by value equality ·
differenceBy — remove by a computed key against another iterable ·
intersectionBy — keep by a computed key shared with another iterable ·
groupBy — keep every element, grouped by key