метод lastIndexWhere
override
Последний индекс в списке, удовлетворяющий заданному test.
Поиск выполняется в списке от индекса start до 0. При первом обнаружении объекта o, для которого test(o) имеет значение true, возвращается индекс o. Если start не указан, по умолчанию используется длина списка.
final notes = <String>['do', 're', 'mi', 're'];
final first = notes.lastIndexWhere((note) => note.startsWith('r')); // 3
final second = notes.lastIndexWhere((note) => note.startsWith('r'),
2); // 1 Возвращает -1, если element не найден.
final notes = <String>['do', 're', 'mi', 're'];
final index = notes.lastIndexWhere((note) => note.startsWith('k'));
print(index); // -1 Реализация
int lastIndexWhere(bool test(E 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 (test(this[i])) return i;
}
return -1;
}
© 2012 the Dart project authors
Licensed under the BSD 3-Clause "New" or "Revised" License.
https://api.dart.dev/stable/2.19.6/dart-collection/ListMixin/lastIndexWhere.html