如何在不摆弄集合的情况下从列表中删除重复项?是否有类似list.distrate()的内容?或list.Unique()?

void main() {
  print("Hello, World!");

  List<String> list = ['abc',"abc",'def'];
  list.forEach((f) => print("this is list $f"));

  Set<String> set = new Set<String>.from(list);
  print("this is #0 ${list[0]}");
  set.forEach((f) => print("set: $f"));

  List<String> l2= new List<String>.from(set);
  l2.forEach((f) => print("This is new $f"));
}
Hello, World!
this is list abc
this is list abc
this is list def
this is #0 abc
set: abc
set: def
This is new abc
This is new def

Set似乎快多了!!但它失go 了项目的顺序:/

推荐答案

我有一个名为Reactive-Dart的库,其中包含许多用于终止和非终止序列的可组合运算符.对于您的场景,它看起来像这样:

final newList = [];
Observable
   .fromList(['abc', 'abc', 'def'])
   .distinct()
   .observe((next) => newList.add(next), () => print(newList));

屈服:

[abc, def]

我应该补充的是,还有其他具有类似功能的库.看看GitHub,我相信你会找到合适的.

Dart相关问答推荐

如何在Telegram Bot中等待用户回复?

为什么 Dart 程序在使用词法作用域时会根据声明变量的位置而表现不同?

`异步内联方法`的这些定义之间有什么区别?

如何在flutter中加载数据之前显示进度对话框?

如何在 Flutter Web 中动态更改 App Title?

如何为屏幕设置不同的主题?

如何从 Polymer Dart 触发自定义事件?

运行时出现Flutter错误:Error waiting for a debug connection: Bad state: No element

Dart JavaScript 与 jQuery 的互操作回调

如何在 Dart 语言中将 Future 转换为 List

如何在Flutter 的http.MultipartRequest请求上上传文件时获取进度事件

GestureDetector onTap 卡

如何判断switch/case中对象的类型?

如何在 Dart 2 中将 List 更改为 List

多次打印同一个字符而不循环

Dart:如何在异步函数中管理并发

Dart 的Expando功能是什么,它有什么作用?

在 Dart 中,List.from 和 .of 以及 Map.from 和 .of 有什么区别?

在 Dart 中,将动态转换为给定类型或返回 null 的语法好方法?

在 Dart 中为数字添加前导零