Flutter List映射获取索引

List映射获取索引

  • 通常用List映射时只能获取到element而不能获取到索引,比如

    return data.map((e) => Media.fromJson(e as Map<String, dynamic>)).toList();
    
  • 当我们要获取到每一个element的索引时,要这样写

    return SizedBox(
          height: 400,
          child: ListView(
            controller: con,
            children: list!
                .asMap()
                .map((i, e) {
                  return MapEntry(
                      i,
                      FilterLocationItem(
                        map: e,
                        index: i,
                        selectedIndex: _selectedIndex,
                      ));
                })
                .values
                .toList(),
          ),
        );
    
  • 其中i就是每一个element的索引

参考: https://blog.csdn.net/jwg1988/article/details/108468178

posted @ 2021-11-18 14:42  R1cardo  阅读(387)  评论(0编辑  收藏  举报