在新版本中使用==
运算符try 重载时, Select 的答案不再有效.现在您需要这样做:
class MyClass {
@override
bool operator ==(other) {
// compare this to other
}
}
但这不安全.other
未指定为类型,可能会发生意外情况.例如:
void main() {
var a = A(1);
var b = B(1);
var result = a == b;
print(result); //result is true
}
class A {
A(this.index);
final int index;
@override
bool operator ==(other) => other.index == index;
}
class B {
B(this.index);
final int index;
}
所以你可以这样做:
class A {
A(this.index);
final int index;
@override
bool operator ==(covariant A other) => other.index == index;
}
你需要用covariant
美元.因为对象重载了==
运算符.
Test Object Type:
visit: hash_and_equals
class A {
A(this.index);
final int index;
@override
bool operator ==(other) => other is A && (other.index == index);
@override
int get hashCode => index;
}