【问题标题】:How to sort a MappedListIterable in Dart如何在 Dart 中对 MappedListIterable 进行排序
【发布时间】:2017-05-09 06:57:33
【问题描述】:

我有一个MappedListIterable 比我知道的排序

当调用排序方法时,我得到了

例外:NoSuchMethodError:类 'MappedListIterable' 没有 实例方法“排序”。接收方:“MappedListIterable”的实例 尝试调用: sort(Closure: (dynamic, dynamic) => dynamic)

【问题讨论】:

    标签: dart dart-html


    【解决方案1】:

    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 有效,但为什么双点?为什么不.sort?你能帮我理解吗?或者,请参考任何文档链接。
    • stackoverflow.com/questions/49447736/….sort() 返回 void(排序就地完成)您不能将结果分配给变量。
    猜你喜欢
    • 1970-01-01
    • 2020-02-10
    • 2012-10-04
    • 2021-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多