test_types_in_equals
Test type of argument in operator ==(Object other)
.
Details
#DO test type of argument in operator ==(Object other)
.
Not testing the type might result in runtime type errors which will be unexpected for consumers of your class.
BAD:
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;
}
}
GOOD:
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;
}
}
Enable
#To enable the test_types_in_equals
rule, add test_types_in_equals
under linter > rules in your analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- test_types_in_equals
If you're instead using the YAML map syntax to configure linter rules, add test_types_in_equals: true
under linter > rules:
analysis_options.yaml
yaml
linter:
rules:
test_types_in_equals: true
Was this page's content helpful?
Thank you for your feedback!
Provide details Thank you for your feedback! Please let us know what we can do to improve.
Provide details Unless stated otherwise, the documentation on this site reflects Dart 3.8.1. Page last updated on 2025-03-07. View source or report an issue.