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'}]