【发布时间】:2017-05-09 06:57:33
【问题描述】:
我有一个MappedListIterable 比我知道的排序
当调用排序方法时,我得到了
例外:NoSuchMethodError:类 'MappedListIterable' 没有 实例方法“排序”。接收方:“MappedListIterable”的实例 尝试调用: sort(Closure: (dynamic, dynamic) => dynamic)
【问题讨论】:
我有一个MappedListIterable 比我知道的排序
当调用排序方法时,我得到了
例外:NoSuchMethodError:类 'MappedListIterable' 没有 实例方法“排序”。接收方:“MappedListIterable”的实例 尝试调用: sort(Closure: (dynamic, dynamic) => dynamic)
【问题讨论】:
在Iterable 上调用.map(f) 后,您会得到一个MappedListIterable。
Iterable class 没有 sort() 方法。这个方法在List上。
因此,您首先需要通过调用.toList() 从您的MappedListIterable 获取List。
var i = [1, 3, 2].map((i) => i + 1);
// i is a MappedListIterable
// you can not call i.sort(...)
var l = i.toList();
l.sort(); // works
或者在一行中(代码高尔夫):
var i = [1, 3, 2].map((i) => i + 1).toList()..sort();
【讨论】:
.sort() 返回 void(排序就地完成)您不能将结果分配给变量。