dart - 如何在 Dart 中删除重复项
问题描述
所以这就是我设置数据的方式
loadNotification(int limit, int offset) async {
List<Notification> notif =
await fetchNotification(http.Client(), limit, offset);
tempNotification.addAll(notif);
_notificationController.add(tempNotification);
}
这是我的Notification()
class Notification {
final String notificationId;
final String notificationTitle;
final String notificationBody;
final String notificationDate;
final String notificationTo;
final String notificationImage;
Notification({
this.notificationId,
this.notificationTitle,
this.notificationBody,
this.notificationDate,
this.notificationTo,
this.notificationImage,
});
factory Notification.fromJson(Map<String, dynamic> json) {
return Notification(
notificationId: json['notificationId'] as String,
notificationTitle: json['notificationTitle'] as String,
notificationBody: json['notificationBody'] as String,
notificationDate: json['notificationDate'] as String,
notificationTo: json['notificationTo'] as String,
notificationImage: json['notificationImage'] as String);
}
}
所以举个例子,我的第一个数据将显示 1,2,3,4,5 然后我单击加载更多它将显示 1,2,3,4,5,3,4,5,6,7。
我已经尝试将我的更改loadNotification
为此
loadNotification(int limit, int offset) async {
List<Notification> notif =
await fetchNotification(http.Client(), limit, offset);
tempNotification.addAll(notif);
filteredNotification = tempNotification.toSet().toList();
_notificationController.add(filteredNotification);
}
但仍然没有帮助,我该如何实现?提前致谢
解决方案
tempNotification.toSet().toList()
无法按预期工作,因为您必须为 Notification 类覆盖 equals 和 hashCode,只有在这种情况下,您才会按值比较,否则按 ref
基于notificationId的一些示例:
class Notification {
final String notificationId;
...
bool operator ==(o) => o is Notification && notificationId == o.notificationId;
int get hashCode => notificationId.hashCode;
}
推荐阅读
- jenkins - 如何从 src 目录中的类中访问共享库资源文件夹中的静态文件
- windows - 有没有一种特殊的方法可以在 Jenkins 中运行可执行文件?
- javascript - 如何删除滚动条,但仍允许使用鼠标滚动?
- mongodb - mongoDB对两个字段排序
- python-3.x - 为什么“请求”模块超时在 python3 中不起作用?
- javascript - 如何在 Javascript 中循环一个简单的 JSON 对象
- scripting - 从 Tcl 中的 string2 中删除 string1 的重复元素
- wmi - 远程 WMI 流量
- typescript - 从graphql接口生成打字稿接口的graphql-codegen问题
- javascript - 过滤文本字段,使其仅支持整数并且在 vue.js 中不为空