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 map 語法配置 Linter 規則,請在 linter > rules 下新增 test_types_in_equals: true
analysis_options.yaml
yaml
linter:
rules:
test_types_in_equals: true