【问题标题】:Iterate with index用索引迭代
【发布时间】:2019-12-12 18:22:49
【问题描述】:

有没有办法遍历可以访问对象和索引的列表? 例如,我有:

Row(
          children: items.map((item) {
            int index = items.indexOf(item);
            return widget(index);
          }).toList()),

我想要直接给我项目及其索引的东西,这样我就不必评估索引:

    int index = items.indexOf(item);

但只写:

Row(
              children: items.functionThatIWant((index,item) {
                return widget(index);
              }).toList()),

【问题讨论】:

标签: list loops dictionary flutter dart


【解决方案1】:

如果你有_current作为你的索引,那么你可以通过做得到所需索引的项目

items.elementAt(_current);

这是假设items确实是List<T>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 2012-08-07
    • 1970-01-01
    • 1970-01-01
    • 2015-04-03
    • 2011-07-23
    • 2015-02-14
    相关资源
    最近更新 更多