我有一個串列,用戶可以在其中動態添加專案。該串列如下所示:
[{author: Tsubasa Yamaguchi, created: 24 Juin 2017, total_chapter: 34, genre: seinen, pic: https://www.nautiljon.com/images/more/03/72/278427.jpg, title: Blue Period}, {author: Tsubasa Yamaguchi, created: 24 Juin 2017, total_chapter: 34, genre: seinen, pic: https://www.nautiljon.com/images/more/03/72/278427.jpg, title: Blue Period}]
我不希望此串列中有任何重復項,因為我在 listView 中顯示專案。我已經嘗試過這種方法,list.toSet().toList()但由于某種原因,我得到了相同的結果。我認為這是因為格式或我的專案' {}'。
有什么建議嗎?
這就是我獲取串列的方式:
FirebaseFirestore firestore = FirebaseFirestore.instance;
List favMangasListTitle = [];
List detailledMangaList = [];
String title = '';
String read_chapter = '';
Future<List> getFavMangas() async {
var value = await firestore.collection("users/${user.uid}/fav_mangas").get();
final favMangasGetter = value.docs.map((doc) => doc.data()).toList();
favMangasListTitle.clear();
detailledMangaList.clear();
for (var i in favMangasGetter) {
title = i['title'];
read_chapter = i['read_chapter'];
favMangasListTitle.add(title);
}
await Future.forEach(favMangasListTitle, (i) async {
final mangas =
await firestore.collection('mangas').where('title', isEqualTo: i).get();
final receivedMangaDetailled =
mangas.docs.map((doc) => doc.data()).toList();
detailledMangaList.addAll(receivedMangaDetailled);
});
var test = detailledMangaList.toSet().toList();
print(test);
return detailledMangaList;
}
uj5u.com熱心網友回復:
至于評論,在title優先考慮的同時,您可以創建一組標題,然后創建一個獨特的地圖。
final data = [...yourData]
final titles = data.map((e) => e["title"]).toSet();
final result = [];
for (final title in titles) {
final item = data.firstWhere((element) =>
element["title"] == title); // include `orElse` if needed
result.add(item);
}
print(result.toString());
uj5u.com熱心網友回復:
您可以使用收藏包并嘗試此操作,這data就是您的清單:
var grouped = groupBy(data, (Map value) => value['title']);
var result = grouped.entries.map((e) => e.value.first).toList();
print("result = $result");
首先,您將串列中的專案按它們分組title,然后在第二行中,我們選擇了每個組中的第一個專案,這為我們提供了一個沒有任何重復的串列name。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/527761.html
標籤:扑列表镖重复
