Home > Blockchain >  how to remove data from List<map> message = [];
how to remove data from List<map> message = [];

Time:12-16

     [

       {
            "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": [],
         
        },

      ]

how can I remove the same booking id data from the list, when I use contains its don't work cause, in this list only booking no are same but other value was different

CodePudding user response:

You can use this method to get your result:

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)); 

change YourListHere with your list.

CodePudding user response:

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

  • Related