跳到主要內容

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 map 語法來配置 linter 規則,請在 linter > rules 下新增 avoid_null_checks_in_equality_operators: true

analysis_options.yaml
yaml
linter:
  rules:
    avoid_null_checks_in_equality_operators: true