如何比较两个相同类型的对象的值?

时间:2019-01-02 13:07:58

标签: dart flutter flutter-test

我需要为flutter项目编写单元测试,如果有一个函数可以遍历相同类型的两个不同对象的所有属性以确保所有值相同,我将很高兴。

代码示例:

go build yourpackage

2 个答案:

答案 0 :(得分:3)

如何覆盖==进行相等性测试

这里是重写==运算符的示例,以便您可以比较两个相同类型的对象。

class Person {
  final String name;
  final int age;
  
  const Person({this.name, this.age});
  
  @override
  bool operator ==(Object other) =>
    identical(this, other) ||
    other is Person &&
    runtimeType == other.runtimeType &&
    name == other.name &&
    age == other.age;

  @override
  int get hashCode => name.hashCode ^ age.hashCode;
}

上面的示例来自this article,建议您使用Equitable软件包来简化此过程。 This article也值得一读。

答案 1 :(得分:0)

您需要覆盖QuizGameState类中的As documented in the manual

相关问题