【发布时间】: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() 是 Flutter 的一部分,并且应该可以在 List 上使用它,因为 List 是 Iterable 抽象类的实现。
任何想法为什么 Flutter 会抛出上述错误或不让我使用它?或者在List 上使用fold() 方法时是否有另一种访问迭代索引的方法?
【问题讨论】:
-
你需要导入
collection.dart -
啊,我明白了,谢谢!
-
当然,欢迎您