How to remove duplicate elements in a list of type model?

Viewed 17

I have a list whose type is model. I have listed the model below. There is also a dropdown list in which I display the data. But I faced the problem that I have duplicate elements. Can you tell me how I can remove duplicate elements?

dropdown

   final List<MyModel> mainList;
   ...
   items: [
          ...mainList.map((item) {
            return DropdownMenuItem(
              value: item,
              child: SizedBox(
                child: Row(

model

class MyModel {
  MyModel ({
    required this.id,
    required this.startedAt,
    required this.name,
  }); 
  late final int id;
  late final String startedAt;
  late final String name;

  MyModel.fromJson(Map<String, dynamic> json){
    id = json['id'];
    startedAt = json['started_at'];
    name= json['name'];
  }

  Map<String, dynamic> toJson() {
    final _data = <String, dynamic>{};
    _data['id'] = id;
    _data['started_at'] = startedAt;
    _data['name'] = name;
    return _data;
  }
}
1 Answers

You can override the operator '==', use something like equatable or use Set instead of List. In a set all item are unique.

Related