美文网首页
flutter Dart语言List如何获取索引值

flutter Dart语言List如何获取索引值

作者: 小小的开发人员 | 来源:发表于2019-12-25 19:16 被阅读0次

其他语言像是js,提供的迭代器是可以直接获取element,index的,但是用dart的map()发现无法获取下标,实际上dart的迭代器只支持获取element自身,想要获得index,就需要借助asMap(),Dart提供的asMap()将列表转换为Map。

List list = ['hello', 'world', 'china'];
final listToMap = list.asMap(); // {0: 'hello', 1: 'world', 2: 'china'}
 List<Widget> _list = _products.asMap().map((i, item){
      return MapEntry(i, _renderCount(item, i)); // 可以在这里获取索引以及渲染组件
    }).values.toList();

Dart提供了List.generate方法获取index

List list = ['hello', 'world', 'china'];
 List result = List.generate(list.length, (index) {
      return _renderItem(index);
});

相关文章

网友评论

      本文标题:flutter Dart语言List如何获取索引值

      本文链接:https://www.haomeiwen.com/subject/odqhoctx.html