list - 如何删除列表的重复项(飞镖/颤振)
问题描述
我有一个基于列表构建 UI 的 futurebuilder,它完成了这项工作,但是由于每当我导航时一次又一次地构建 UI,我得到了重复。我的问题是,Dart 中是否有一种先天方法可以从列表中删除重复项?我已经尝试过这个StackOverflow 问题,但是它不起作用。
这是我的自定义模型:
class HomeList {
Widget navigateScreen;
String imagePath;
PatientInfo patientInfo;
HomeList({
this.navigateScreen,
this.imagePath = '',
this.patientInfo,
});
static List<HomeList> homeList = [];
}
这是我从 cloud_firestore 获取数据的 futureBuilder 函数:
_getPatients() async {
if (didLoadpatients == 0) {
print('this is didloadpatients at start of func $didLoadpatients');
var document = await db
.collection('users')
.document(mUser.uid)
.collection('patients');
document.getDocuments().then((QuerySnapshot query) async {
query.documents.forEach((f) {
uids.add(f.data['uID']);
});
didLoadpatients++;
print('this is didloadpatients at end of func $didLoadpatients');
for (var i = 0; i < uids.length; i++) {
var userDocuments = await db.collection('users').document(uids[i]);
userDocuments.get().then((DocumentSnapshot doc) {
print(doc.data);
homeList.add(HomeList(
imagePath: 'assets/fitness_app/fitness_app.png',
patientInfo: new PatientInfo.fromFbase(doc.data)));
});
print(homeList);
}
});
} else
print('I am leaving the get patient function');
}
Future<bool> getData() async {
_getCurrentUser();
await Future.delayed(const Duration(milliseconds: 1500), () async {
_getPatients();
});
return true;
}
任何帮助将不胜感激,谢谢!
解决方案
要删除重复项,您可以使用 Set Data Structure 而不是 List。
只需使用 Set 而不是 List 来获取唯一值。
推荐阅读
- deep-linking - 使用深层链接将消息发布到特定的 MS Teams 频道
- python - 不使用 GEKKO 的多元非线性回归
- uwp - Microsoft UWP 是否支持 gRPC?
- javascript - 将 Json 数组键值转换为 js 数组
- reactjs - 我可以在卡内使用反应引导表吗?
- r - R lengend 'topright' 在 parmfrow=c(1,2) 设置中不起作用
- python - 如何获取python脚本的名称
- java - 如何将回调中的值传递给另一个方法
- jquery - 在 AJAX 调用中使用 Promise 时未定义函数
- c++ - 如何在win32 C++控制台应用程序中调用uwp类库