跳到主要内容

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