Sil*_*der 14
使用toset然后toList
var ids = [1, 4, 4, 4, 5, 6, 6];
var distinctIds = ids.toSet().toList();
Run Code Online (Sandbox Code Playgroud)
您需要一个 LinkedSet 来仅包含唯一值并保持插入顺序,但目前我们在 dart 中没有它。但是,您可以使用 LinkedHashMap 模拟 LinkedSet:
var input = ["apple", "orange", "cherries", "pears", "apple", "apple", "orange"];
var uniques = new LinkedHashMap<String, bool>();
for (var s in input) {
uniques[s] = true;
}
for (var key in uniques.getKeys()) {
print ("$key");
}
Run Code Online (Sandbox Code Playgroud)