0

I have this data:

[
  { "name": "Eric", "origin": "Tristan da Cunha" },
  { "name": "Eric", "origin": "Tristan da Cunha" },
  { "name": "Adelaide", "origin": "Cayman Islands" }
]

There is a duplicated data, how do I remove it before converting to a list?

iqfareez
  • 555
  • 7
  • 21
  • 1
    Does this answer your question? [How can I delete duplicates in a Dart List? list.distinct()?](https://stackoverflow.com/questions/12030613/how-can-i-delete-duplicates-in-a-dart-list-list-distinct) – Md. Yeasin Sheikh Feb 07 '22 at 05:00

3 Answers3

1

First, make a class for the data, include the hashCode and operator overrides. toString override is optional but would be helpful:

class DataModel {
  String? name;
  String? origin;

  DataModel({this.name, this.origin});

  @override
  int get hashCode => '$name, $origin'.hashCode;

  // this will do the magic
  @override
  bool operator ==(Object other) {
    return other is DataModel &&
        name.toString() == other.name.toString() &&
        origin.toString() == other.origin.toString();
  }

  @override
  String toString() {
    return "{name: '$name', origin: '$origin'}";
  }
}

Then, anywhere in your Dart code:

import 'data_model.dart';

void main(List<String> arguments) {
  // notice that Eric is duplicated in this entry
  List<DataModel> filterData = [
    DataModel(name: 'Eric', origin: "Tristan da Cunha"),
    DataModel(name: 'Eric', origin: "Tristan da Cunha"),
    DataModel(name: 'Adelaide', origin: "Cayman Islands"),
  ];

  // toSet() will remove the redundancy, then convert it back to toList()
  var removedDuplicatedDatas = filterData.toSet().toList();
  print(removedDuplicatedDatas);
}

Output:

[{name: 'Eric', origin: 'Tristan da Cunha'}, {name: 'Adelaide', origin: 'Cayman Islands'}]
iqfareez
  • 555
  • 7
  • 21
0

Remove Item by it's name

 final ids = uniqueLedgerList.map((e) => e["name"]).toSet();
 uniqueLedgerList.retainWhere((x) => ids.remove(x["name"]));
                                            
 log("Remove Duplicate Items--->$uniqueLedgerList");
Aamil Silawat
  • 7,735
  • 3
  • 19
  • 37
0

You can do simply using this

  var listwithDuplicates = listwithDuplicates.toSet().toList();
  print(listwithDuplicates);