目录

avoid_null_checks_in_equality_operators

Don't check for null in custom == operators.

此规则自 Dart 2.0 版本起可用。

此规则提供 快速修复

详情

#

NOTE: This lint has been replaced by the non_nullable_equals_parameter warning and is deprecated. Remove all inclusions of this lint from your analysis options.

DON'T check for null in custom == operators.

As null is a special value, no instance of any class (other than Null) can be equivalent to it. Thus, it is redundant to check whether the other instance is null.

BAD:

dart
class Person {
  final String? name;

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

GOOD:

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