Search code examples
flutterdartduplicatesunique

Flutter filter json data to unique data


  "bookings":[
     {
        "id":"600bf6d4057c5a06d373557d",
        "orderId":"600bf6d4057c5a06d373557c",
        "carId":"600bf505057c5a06d3735579",
        "car":{
           "id":"600bf505057c5a06d3735579",
           "name":"car 1",
           "description":"yhis car",
           "numberOfSeats":1,
           "position":[
              -1.051178,
              37.0951137
           ],
           "pricePerDay":1.0,
           "pickupLocation":{
              "locationName":"Thika",
              "position":[
                 -1.051178,
                 37.0951137
              ],
              "radius":0.0
           },
           "dropoffLocation":null,
           "rating":0.0,
           "category":"Casual",
           "numberPlate":"kbu",
           "ratingsCount":0,
           "verified":true,
           "subCategory":"Sedan/Coupe",
           "tag":null,
           "available":true,
           "type":null,
           "bookings":null,
           "additionalFeatures":[
              {
                 "name":"this",
                 "price":0.0
              },
              {
                 "name":"this",
                 "price":0.0
              }
           ],
           "ownerId":"600bf22e057c5a06d3735571",
           "carOwner":null,
           "locationName":"Thika",
           "approvedBy":"[email protected]",
           "approvedOn":"2021-01-23T10:12:48.799+00:00",
           "timestamp":null
        },
        "userId":"600bf2c3057c5a06d3735573",
        "carOwner":null,
        "ownerId":"600bf22e057c5a06d3735571",
        "date":"2021-01-24T00:00:00.000+00:00",
        "timestamp":"2021-01-23T10:13:40.561+00:00"
     },
     {
        "id":"600bf6d4057c5a06d373557e",
        "orderId":"600bf6d4057c5a06d373557c",
        "carId":"600bf505057c5a06d3735579",
        "car":{
           "id":"600bf505057c5a06d3735579",
           "name":"car 1",
           "description":"yhis car",
           "numberOfSeats":1,
           "position":[
              -1.051178,
              37.0951137
           ],
           "pricePerDay":1.0,
           "pickupLocation":{
              "locationName":"Thika",
              "position":[
                 -1.051178,
                 37.0951137
              ],
              "radius":0.0
           },
           "dropoffLocation":null,
           "rating":0.0,
           "category":"Casual",
           "numberPlate":"kbu",
           "ratingsCount":0,
           "verified":true,
           "subCategory":"Sedan/Coupe",
           "tag":null,
           "available":true,
           "type":null,
           "bookings":null,
           "additionalFeatures":[
              {
                 "name":"this",
                 "price":0.0
              },
              {
                 "name":"this",
                 "price":0.0
              }
           ],
           "ownerId":"600bf22e057c5a06d3735571",
           "carOwner":null,
           "locationName":"Thika",
           "approvedBy":"[email protected]",
           "approvedOn":"2021-01-23T10:12:48.799+00:00",
           "timestamp":null
        },
        "userId":"600bf2c3057c5a06d3735573",
        "carOwner":null,
        "ownerId":"600bf22e057c5a06d3735571",
        "date":"2021-01-25T00:00:00.000+00:00",
        "timestamp":"2021-01-23T10:13:40.564+00:00"
     },
     {
        "id":"600bf6d4057c5a06d373557f",
        "orderId":"600bf6d4057c5a06d373557c",
        "carId":"600bf505057c5a06d3735579",
        "car":{
           "id":"600bf505057c5a06d3735579",
           "name":"car 1",
           "description":"yhis car",
           "numberOfSeats":1,
           "position":[
              -1.051178,
              37.0951137
           ],
           "pricePerDay":1.0,
           "pickupLocation":{
              "locationName":"Thika",
              "position":[
                 -1.051178,
                 37.0951137
              ],
              "radius":0.0
           },
           "dropoffLocation":null,
           "rating":0.0,
           "category":"Casual",
           "numberPlate":"kbu",
           "ratingsCount":0,
           "verified":true,
           "subCategory":"Sedan/Coupe",
           "tag":null,
           "available":true,
           "type":null,
           "bookings":null,
           "additionalFeatures":[
              {
                 "name":"this",
                 "price":0.0
              },
              {
                 "name":"this",
                 "price":0.0
              }
           ],
           "ownerId":"600bf22e057c5a06d3735571",
           "carOwner":null,
           "locationName":"Thika",
           "approvedBy":"[email protected]",
           "approvedOn":"2021-01-23T10:12:48.799+00:00",
           "timestamp":null
        },
        "userId":"600bf2c3057c5a06d3735573",
        "carOwner":null,
        "ownerId":"600bf22e057c5a06d3735571",
        "date":"2021-01-26T00:00:00.000+00:00",
        "timestamp":"2021-01-23T10:13:40.565+00:00"
     },
     {
        "id":"600bf6d4057c5a06d3735580",
        "orderId":"600bf6d4057c5a06d373557c",
        "carId":"600bf61c057c5a06d373557b",
        "car":{
           "id":"600bf61c057c5a06d373557b",
           "name":"csr 2",
           "description":"this",
           "numberOfSeats":1,
           "position":[
              -1.2163932,
              36.8949246
           ],
           "pricePerDay":1.0,
           "pickupLocation":{
              "locationName":"Thika Road",
              "position":[
                 -1.2163932,
                 36.8949246
              ],
              "radius":0.0
           },
           "dropoffLocation":null,
           "rating":0.0,
           "category":"Business",
           "numberPlate":"kbu",
           "ratingsCount":0,
           "verified":true,
           "subCategory":"Tours",
           "tag":null,
           "available":true,
           "type":null,
           "bookings":null,
           "additionalFeatures":[
              {
                 "name":"heey",
                 "price":0.0
              }
           ],
           "ownerId":"600bf22e057c5a06d3735571",
           "carOwner":null,
           "locationName":"Thika Road",
           "approvedBy":"[email protected]",
           "approvedOn":"2021-01-23T10:12:22.058+00:00",
           "timestamp":null
        },
        "userId":"600bf2c3057c5a06d3735573",
        "carOwner":null,
        "ownerId":"600bf22e057c5a06d3735571",
        "date":"2021-01-24T00:00:00.000+00:00",
        "timestamp":"2021-01-23T10:13:40.566+00:00"
     },
     {
        "id":"600bf6d4057c5a06d3735581",
        "orderId":"600bf6d4057c5a06d373557c",
        "carId":"600bf61c057c5a06d373557b",
        "car":{
           "id":"600bf61c057c5a06d373557b",
           "name":"csr 2",
           "description":"this",
           "numberOfSeats":1,
           "position":[
              -1.2163932,
              36.8949246
           ],
           "pricePerDay":1.0,
           "pickupLocation":{
              "locationName":"Thika Road",
              "position":[
                 -1.2163932,
                 36.8949246
              ],
              "radius":0.0
           },
           "dropoffLocation":null,
           "rating":0.0,
           "category":"Business",
           "numberPlate":"kbu",
           "ratingsCount":0,
           "verified":true,
           "subCategory":"Tours",
           "tag":null,
           "available":true,
           "type":null,
           "bookings":null,
           "additionalFeatures":[
              {
                 "name":"heey",
                 "price":0.0
              }
           ],
           "ownerId":"600bf22e057c5a06d3735571",
           "carOwner":null,
           "locationName":"Thika Road",
           "approvedBy":"[email protected]",
           "approvedOn":"2021-01-23T10:12:22.058+00:00",
           "timestamp":null
        },
        "userId":"600bf2c3057c5a06d3735573",
        "carOwner":null,
        "ownerId":"600bf22e057c5a06d3735571",
        "date":"2021-01-25T00:00:00.000+00:00",
        "timestamp":"2021-01-23T10:13:40.567+00:00"
     }
  ],

Helo have face a slight issue which is eating up my head. i have such json as shown above. but if you look closely the first 3 cars are duplicates and the last 2 are aslo the same i want to have a way that i will have no duplicates such that am going to have a list with only the two cars being unique. I want the data to be displaayed on my app to be having a length o two cars


Solution

  • take your list and filter using where

                  final existing = Set<String>();
    
    
     bookings.where((person) => existing.add(person.car.id))
            .toList();
    
    print('unique $unique');
    // unique [Instance of 'Bookings', Instance of 'Bookings']