[
{
"booking_no": "HC60398",
"sender_name": "Tanvir",
"receiver_name": "House",
"sender_image": "image link",
"receiver_image": "image link",
"chat_id": "2",
"message": "hlw",
"files": [],
},
{
"booking_no": "HC60398",
"sender_name": "Tanvir",
"receiver_name": "House",
"sender_image": "image link",
"receiver_image": "image link",
"chat_id": "2",
"message": "hi",
},
{
"booking_no": "HC60399",
"sender_name": "Tanvir",
"receiver_name": "House",
"sender_image": "image link",
"receiver_image": "image link",
"chat_id": "2",
"message": "hlw",
"files": [],
},
]
如何从列表中删除相同的预订id数据,当我使用包含其不工作的原因,在这个列表中只有预订no是相同的,但其他值是不同的
您可以使用这个方法来获得您的结果:
List getNewListWithuotOccurencesBookingNo(List list) {
Map map = {};
for(int index = 0; index < list.length; index += 1) {
final current = list[index];
final id = current["booking_no"];
if(map[id] != null){
continue;
}
map[id] = current;
}
return map.values.toList();
}
print(getNewListWithuotOccurencesBookingNo(YourListHere));
用你的列表改变YourListHere
尝试:
final list= [
{
"booking_no": "HC60398",
"sender_name": "Tanvir",
"receiver_name": "House",
"sender_image": "image link",
"receiver_image": "image link",
"chat_id": "2",
"message": "hlw",
"files": [],
},
{
"booking_no": "HC60398",
"sender_name": "Tanvir",
"receiver_name": "House",
"sender_image": "image link",
"receiver_image": "image link",
"chat_id": "2",
"message": "hi",
},
{
"booking_no": "HC60399",
"sender_name": "Tanvir",
"receiver_name": "House",
"sender_image": "image link",
"receiver_image": "image link",
"chat_id": "2",
"message": "hlw",
"files": [],
},
];
final filterdList = [];
for(final item in list){
if (!filterdList.any((i) => i["booking_no"] == item["booking_no"])){
filterdList.add(item);
}
}
print(filterdList.length); // Output 2