dart - 如何在 Dart 中对 MappedListIterable 进行排序

标签 dart dart-html

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

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

EXCEPTION: NoSuchMethodError: Class 'MappedListIterable' has no instance method 'sort'. Receiver: Instance of 'MappedListIterable' Tried calling: sort(Closure: (dynamic, dynamic) => dynamic)

最佳答案

你在调用 .map(f) 后得到一个 MappedListIterableIterable 上。

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();

关于dart - 如何在 Dart 中对 MappedListIterable 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41303656/

相关文章:

dart - 按属性选择元素后代

dart - 如何使用 ScriptProcessorNode

javascript - AngularDart 4-5 变更检测与 Angular 5 变更检测有何不同?

dart - 导入的库{}和{}都不能被无名表示什么?

dart - 父子自定义元素之间的数据访问

dart - 如何使用 Dart 在 IndexedDB 中使用复合键创建对象存储

visual-studio - 如何 "Import in flutterTest Widget"

firebase - 使用 runTransaction 的 Flutter/Firebase Firestore 什么也不做

在 Firefox 中使用 Dart 进行地理定位 getCurrentPosition 不是函数

dart - 在 Dart 中传递 Canvas 的 2D 上下文