Esta página ainda não foi traduzida, por isso é exibida em inglês. Ajude a traduzir

where

Returns a lazy iterable of the elements a predicate returns true for.

Iterable<A> where<A>(bool Function(A a) f, Iterable<A> iterable) FxAsyncIterable<A> whereAsync<A>(FutureOr<bool> Function(A a) f, FxAsyncIterable<A> iterable) Fx<T> Fx.where(bool Function(T a) f) // chain FxAsync<T> FxAsync.where(FutureOr<bool> Function(T a) f) Iterable<A> filter<A>(bool Function(A a) f, Iterable<A> iterable) // FxTS alias

Lecture

where is the other fundamental transformer alongside map: it keeps every element for which the predicate returns true and drops the rest, lazily. Nothing runs until a terminal operator pulls values through. where is the Dart-idiomatic name; fxdart also accepts the FxTS spelling filter — they're the same operator.

Because it's lazy, combining where with take only evaluates as many predicate calls as needed to satisfy the take — see Demo 1.

whereAsync has its own dedicated concurrent implementation (rather than reusing mapAsync's): when you attach .concurrent(n), n predicates are evaluated in parallel, but the elements that pass are still released downstream in their original order — concurrency changes throughput, never the result.

Demo 1 · Basics & laziness

Demo 2 · Async, with concurrency

Try it yourself

Exercise: use where to keep only ages 18 and over.

Related: reject — the opposite of filter · compact — filter out nulls · map — transform instead of keep/drop · concurrent — parallel evaluation