跳至主要内容

test_types_in_equals

在 '==' 中缺少 '{0}' 的类型测试。

描述

#

== 运算符的重载没有包含对参数值的类型测试时,分析器会生成此诊断。

示例

#

以下代码会生成此诊断,因为 other 未进行类型测试

dart
class C {
  final int f;

  C(this.f);

  @override
  bool operator ==(Object other) {
    return (other as C).f == f;
  }
}

常见修复

#

在计算返回值时进行 is 测试

dart
class C {
  final int f;

  C(this.f);

  @override
  bool operator ==(Object other) {
    return other is C && other.f == f;
  }
}