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

indexWhere

Returns the index of the first element for which a predicate is true, or -1.

int indexWhere<A>(bool Function(A a) f, Iterable<A> iterable) Future<int> indexWhereAsync<A>(FutureOr<bool> Function(A a) f, FxAsyncIterable<A> iterable) int Fx.indexWhere(bool Function(T a) f) // chain Future<int> FxAsync.indexWhere(FutureOr<bool> Function(T a) f) // chain int findIndex<A>(bool Function(A a) f, Iterable<A> iterable) // FxTS alias

Lecture

indexWhere is the Dart-idiomatic name (cf. List.indexWhere); fxdart also accepts the FxTS spelling findIndex — they're the same operator. It's find's sibling: same lazy, short-circuiting scan, but it reports where the match was instead of the match itself. Note the different "nothing found" sentinel — this one is -1, an int, matching JavaScript's Array.prototype.findIndex. That's a deliberate contrast with head/find/nth, which all use null: an index has a natural "impossible" value, so FxDart keeps the FxTS convention here instead of inventing an int? return type.

Internally it pairs each element with its index (via zipWithIndex) and stops at the first one that passes f.

Demo 1 · Basics

Demo 2 · Short-circuiting & async

Try it yourself

Exercise: use indexWhere to find bob's position in the queue, or -1.

Related: find — same search, returns the value · includes — just "is it there?" · nth — the inverse: index in, value out · zipWithIndex — what powers this under the hood