跳到主要內容

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