【问题标题】:Dart/Flutter: Method 'foldIndexed' isn't defined for the type 'List'Dart/Flutter:没有为“List”类型定义方法“foldIndexed”
【发布时间】:2021-10-04 08:37:31
【问题描述】:

我正在尝试在 Flutter/Dart 中对 List 使用 foldIndexed() 方法,如下所示:

// should only compute the combined character length of the first two fruits
['apple', 'banana', 'cherry'].foldIndexed(
    0,
    (index, previousElement, element) =>
        index < 2 ? previousElement + element.length : previousElement);

由于某种原因,它显示以下语法错误:The method 'foldIndexed' isn't defined for the type 'List'.

documentation 我了解到 foldIndexed() 是 Fl​​utter 的一部分,并且应该可以在 List 上使用它,因为 ListIterable 抽象类的实现。

任何想法为什么 Flutter 会抛出上述错误或不让我使用它?或者在List 上使用fold() 方法时是否有另一种访问迭代索引的方法

【问题讨论】:

  • 你需要导入collection.dart
  • 啊,我明白了,谢谢!
  • 当然,欢迎您

标签: list flutter dart fold


【解决方案1】:

我的问题是我忘记导入 @pskink 指出的 collection.dart

【讨论】:

    猜你喜欢
    • 2021-08-21
    • 2020-10-01
    • 1970-01-01
    • 2021-03-04
    • 2019-08-14
    • 2020-10-23
    • 1970-01-01
    • 1970-01-01
    • 2020-06-06
    相关资源
    最近更新 更多