Removing non unique Objects from a List (identifiable by a property) Removing non unique Objects from a List (identifiable by a property) dart dart

Removing non unique Objects from a List (identifiable by a property)


First of all you would need to define by which criteria the objects are supposed to be unique. Is there an identifying property for example? If that is the case the following options should work.

The most efficient way is probably to make the approach with a set working. For that you would need to turn your objects into data objects, meaning have them identify for equality by property values. For that you would override the equality operator and hashcode methods. However this changes how your objects behave on every equality operation. So you would have to judge if that is suitable. See this article.

Another option is to just filter manually using a map:

class MyObj {  String val;  MyObj(this.val);}TestListFiltering(){  List<MyObj> l = [    MyObj("a"),    MyObj("a"),      MyObj("b"),  ];  // filter list l for duplicate values of MyObj.val property  Map<String, MyObj> mp = {};  for (var item in l) {    mp[item.val] = item;  }  var filteredList = mp.values.toList();}