avoid_null_checks_in_equality_operators
不要在自訂 ==
運算子中檢查 null
。
此規則自 Dart 2.0 起可用。
此規則提供快速修正。
詳細資訊
#注意: 此 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
除非另有說明,否則本網站上的文件反映了 Dart 3.6.0。頁面上次更新於 2024-07-03。 檢視原始碼 或 回報問題。