lastIndexOf method Null safety

int lastIndexOf (
  1. Object? element,
  2. [int? start]
)
override

Returns the last index of element in this list.

Searches the list backwards from index start to 0.

The first time an object o is encountered so that o == element, the index of o is returned.

List<String> notes = ['do', 're', 'mi', 're'];
notes.lastIndexOf('re', 2); // 1

If start is not provided, this method searches from the end of the list.

notes.lastIndexOf('re');  // 3

Returns -1 if element is not found.

notes.lastIndexOf('fa');  // -1

Implementation

int lastIndexOf(Object? element, [int? start]) {
  if (start == null || start >= this.length) start = this.length - 1;

  // TODO(38493): The previous line should promote.
  if (start == null) throw "!";

  for (int i = start; i >= 0; i--) {
    if (this[i] == element) return i;
  }
  return -1;
}