lastIndexOf 方法
override
该列表中 element 的最后一个索引。
从索引 start 向后搜索列表。
遇到第一个满足 o == element 的对象 o,返回 o 的索引。
final notes = <String>['do', 're', 'mi', 're'];
const startIndex = 2;
final index = notes.lastIndexOf('re', startIndex); // 1
如果没有提供 start,则从列表末尾开始搜索。
final notes = <String>['do', 're', 'mi', 're'];
final index = notes.lastIndexOf('re'); // 3
如果未找到 element,则返回 -1。
final notes = <String>['do', 're', 'mi', 're'];
final index = notes.lastIndexOf('fa'); // -1
实现
int lastIndexOf(Object? element, [int? start]) {
if (start == null || start >= this.length) start = this.length - 1;
for (int i = start; i >= 0; i--) {
if (this[i] == element) return i;
}
return -1;
}