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

1

10

I am capturing events from a stream, each event is a Device Object. The way the stream work is that it is on a timer, so it picks up the same device multiple times and adds to the stream.

I am putting all theres is a List<Device> and putting that into another stream.

I have create a StreamTransformer in the attempt to remove duplicate from the list and then add the unique list back into the stream.

This transform code below, I have tried to add to set and back to list, but this hasn't worked I assume due to the fact they are objects not strings.

  //Transform Stream List by removing duplicate objects
  final deviceList = StreamTransformer<List<Device>, List<Device>>.fromHandlers(
      handleData: (list, sink) {
      List<Device> distinctList = list.toSet().toList();
      sink.add(distinctList);
  });

I have attempted to use .where and other libraries but to no avail and am hoping for some guidance.

Device Object contains unique id and name that could be used to filter out duplicates

Question: How can I remove duplicate objects from a List in Dart?

Thanks in advance.

Flanagan answered 19/1, 2019 at 23:44 Comment(1)
@Günter Zöchbauer This one is for you mate :)Flanagan
E
18

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 getter. 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();
}
Eleonoreleonora answered 20/1, 2019 at 0:50 Comment(2)
The second option worked great. Thanks Oswin, you are a scholar and a gentleman.Flanagan
Very helpful! Thanks! I can finally relax now lol youtube.com/watch?v=MrHxhQPOO2cHuzzah

© 2022 - 2024 — McMap. All rights reserved.