Dart/Flutter:未为类型“List”定义方法“foldIndexed”

Jon*_*oni 2 list fold dart flutter

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

// 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);
Run Code Online (Sandbox Code Playgroud)

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

从这个文档中我了解到这foldIndexed()是 Flutter 的一部分,并且应该可以在抽象类的实现List上使用它。ListIterable

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