【问题标题】:How to check list of objects are equal?如何检查对象列表是否相等?
【发布时间】:2022-08-22 14:39:08
【问题描述】:

我有一个学生班

class Student{
  
  String? name;
  
  String? section;
  
  Student(this.name,this.section);
  
  @override
  bool operator == (Object other){
    return other is Student && other.name == name && other.section == section;
  }
  
  @override
  int get hashCode => name.hashCode & section.hashCode;
  
}

  List<Student> studentsOne = [
    Student(\"maverick\",\"A\"),
    Student(\"roger\",\"A\"),
    Student(\"kenny\",\"B\"),
    Student(\"kooper\",\"A\")
  ];
  
   List<Student> studentsTwo = [
    Student(\"maverick\",\"A\"),
    Student(\"roger\",\"A\"),
    Student(\"kenny\",\"B\"),
    Student(\"kooper\",\"A\")
  ];
  
  print(studentsOne == studentsTwo); // prints false

你能帮忙的话,我会很高兴。提前致谢

标签: flutter list dart collections compare


【解决方案1】:

您可以使用package:collection/collection.dart

List<Student> studentsOne = [
  Student("maverick","A"),
  Student("roger","A"),
  Student("kenny","B"),
  Student("kooper","A")
];

List<Student> studentsTwo = [
  Student("maverick","A"),
  Student("roger","A"),
  Student("kenny","B"),
  Student("kooper","A")
];

if (const DeepCollectionEquality().equals(studentsOne, studentsTwo)) {
  print('studentsOne and studentsTwo are equal')
}

【讨论】:

    猜你喜欢
    • 2019-11-04
    • 2021-04-07
    • 2021-04-27
    • 2016-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多