indexWhere
Returns the index of the first element for which a predicate is true, or -1.
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.
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