跳到主要内容

avoid_null_checks_in_equality_operators

稳定
修复可用

在自定义的 == 运算符中不要检查 null

详情

#

注意:此 lint 已被 non_nullable_equals_parameter 警告取代,并且已弃用。请从您的分析选项中移除所有包含此 lint 的配置。

不要 在自定义的 == 运算符中检查 null

由于 null 是一个特殊值,任何类的实例(除了 Null 本身)都不可能与它相等。因此,检查另一个实例是否为 null 是多余的。

不好的示例

dart
class Person {
  final String? name;

  @override
  operator ==(Object? other) =>
      other != null && other is Person && name == other.name;
}

好的示例

dart
class Person {
  final String? name;

  @override
  operator ==(Object? other) => other is Person && name == other.name;
}

启用

#

要启用 avoid_null_checks_in_equality_operators 规则,请在你的 analysis_options.yaml 文件中的 linter > rules 下添加 avoid_null_checks_in_equality_operators

analysis_options.yaml
yaml
linter:
  rules:
    - avoid_null_checks_in_equality_operators

如果你使用的是 YAML 映射语法来配置 linter 规则,请在 linter > rules 下添加 avoid_null_checks_in_equality_operators: true

analysis_options.yaml
yaml
linter:
  rules:
    avoid_null_checks_in_equality_operators: true