indexWhere method

int indexWhere(
  1. Predicate<T> test, [
  2. int start = 0
])

Returns the first index in the list that satisfies the provided test.

Searches the list from index start to the end of the list. The first time an object obj is encountered so that test(obj) is true, the index of obj is returned.

final IList<String> notes = ['do', 're', 'mi', 're'].lock;
notes.indexWhere((note) => note.startsWith('r'));       // 1
notes.indexWhere((note) => note.startsWith('r'), 2);    // 3

Returns -1 if element is not found.

notes.indexWhere((note) => note.startsWith('k'));       // -1

Implementation

int indexWhere(Predicate<T> test, [int start = 0]) {
  var _length = length;
  if (_length == 0) return -1;
  if (start < 0 || start >= _length)
    throw ArgumentError.value(start, "index", "Index out of range");
  for (int i = start; i <= _length - 1; i++) if (test(this[i])) return i;
  return -1;
}