当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Dart List.lastIndexWhere用法及代码示例


dart:core 库中List.lastIndexWhere 方法的用法介绍如下。

用法:

int lastIndexWhere(
   bool test(
   E element   
),    
   [int? start]   
)

列表中满足提供的 test 的最后一个索引。

从索引 start 到 0 搜索列表。第一次遇到对象 o 以使 test(o) 为真时,返回 o 的索引。如果省略start,则默认为列表的length

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

如果未找到 element,则返回 -1。

final notes = <String>['do', 're', 'mi', 're'];
final index = notes.lastIndexWhere((note) => note.startsWith('k'));
print(index); // -1

相关用法


注:本文由纯净天空筛选整理自dart.dev大神的英文原创作品 lastIndexWhere method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。