このページはまだ翻訳されていないため、英語で表示されます。 翻訳に参加する

contains

Returns true when an iterable contains a value, compared with ==.

bool Fx.contains(Object? a) // chain — inherited from Iterable (Dart idiom) bool includes<A>(A a, Iterable<A> iterable) // top-level: no `contains` (collides with package:test) Future<bool> includesAsync<A>(A a, FxAsyncIterable<A> iterable) // top-level async

Lecture

contains is the Dart-idiomatic name for membership testing, and on a chain you already have it: Fx inherits .contains() from Iterable, so fx(xs).contains(a) just works. There is no top-level contains function, though — the name collides with package:test's matcher — so the data-first form keeps its FxTS spelling includes(a, iterable), which is literally iterable.contains(a). The async version, includesAsync, is built on top of someAsync (b == a as the predicate), which means it inherits the same short-circuiting: it stops pulling from the source the moment it finds a match.

Equality is checked with Dart's ==, so it respects any operator == override on your own classes — it isn't limited to primitives.

Demo 1 · Basics

Demo 2 · Async, and proof it short-circuits

Only 2 of 5 elements are pulled before includesAsync stops:

Try it yourself

Exercise: use contains to check whether requestId is allowed.

Related: some — what includesAsync is built from · find — get the matching value, not just a bool · findIndex — get the position instead · isEmpty — the other value-based check nearby