The lastIndexWhere()
method is used to find the index of the last occurrence of an element that satisfies the given condition.
int lastIndexWhere (bool test(E element), [ int start = 0 ]);
It accepts a condition that can be searched in the list.
The method lastIndexWhere()
returns the last index in the list that matches the given condition.
Note: It searches the list from the beginning of the index to the end. The index of the item is returned if found. Otherwise, it returns -1.
The following code shows how to use the method lastIndexWhere()
in Dart:
void main() {// Creating listfinal List<Map<String, dynamic>> _student = [{"name": "Elisa", "id": "15GH023", "offeredCourse": 10},{"name": "Maria", "id": "15GH086", "offeredCourse": 8},{"name": "Joe", "id": "15GH007", "offeredCourse": 11},];// Find the last index where offeredCourse > 8final foundIndex = _student.lastIndexWhere((e) => e["offeredCourse"] > 8);// Display resultif (foundIndex != -1) {print("Index $foundIndex: ${_student[foundIndex]}");}}
_student
.lastIndexWhere()
to find the last index where offeredCourse > 8
. It returns the index where offeredCourse > 8
, which is then stored in variable foundIndex
.