skip to Main Content
     [

       {
            "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

2

Answers


  1. Try:

    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
    
    
    Login or Signup to reply.
  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search