lastIndexWhere method

int lastIndexWhere(
  1. Predicate<T> test, [
  2. int? start
])

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

Searches the list from index start to 0. The first time an object obj is encountered such that test(obj) is true, the index of obj is returned. If start is omitted, it defaults to the length of the list.

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

Returns -1 if element is not found.

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

Implementation

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