test_types_in_equals
測試 operator ==(Object other)
中引數的類型。
詳細資訊
#務必 測試 operator ==(Object other)
中引數的類型。
未測試類型可能會導致執行階段類型錯誤,這對您的類別使用者來說是意想不到的。
錯誤範例
dart
class Field {
}
class Bad {
final Field someField;
Bad(this.someField);
@override
bool operator ==(Object other) {
Bad otherBad = other as Bad; // LINT
bool areEqual = otherBad != null && otherBad.someField == someField;
return areEqual;
}
@override
int get hashCode {
return someField.hashCode;
}
}
正確範例
dart
class Field {
}
class Good {
final Field someField;
Good(this.someField);
@override
bool operator ==(Object other) {
if (identical(this, other)) {
return true;
}
return other is Good &&
this.someField == other.someField;
}
@override
int get hashCode {
return someField.hashCode;
}
}
啟用
#若要啟用 test_types_in_equals
規則,請在您的 analysis_options.yaml
檔案中,於 **linter > rules** 下新增 test_types_in_equals
analysis_options.yaml
yaml
linter:
rules:
- test_types_in_equals
如果您改用 YAML 對應語法來設定 linter 規則,請在 **linter > rules** 下新增 test_types_in_equals: true
。
analysis_options.yaml
yaml
linter:
rules:
test_types_in_equals: true
除非另有說明,否則本網站的文件反映的是 Dart 3.7.1 版本。頁面最後更新於 2025-03-07。 查看原始碼 或 回報問題。