avoid_null_checks_in_equality_operators
Don't check for null
in custom ==
operators.
Details
#NOTE: This lint has been replaced by the non_nullable_equals_parameter
warning and is deprecated. Remove all inclusions of this lint from your analysis options.
DON'T check for null
in custom ==
operators.
As null
is a special value, no instance of any class (other than Null
) can be equivalent to it. Thus, it is redundant to check whether the other instance is null
.
BAD:
class Person {
final String? name;
@override
operator ==(Object? other) =>
other != null && other is Person && name == other.name;
}
GOOD:
class Person {
final String? name;
@override
operator ==(Object? other) => other is Person && name == other.name;
}
Enable
#To enable the avoid_null_checks_in_equality_operators
rule, add avoid_null_checks_in_equality_operators
under linter > rules in your analysis_options.yaml
file:
linter:
rules:
- avoid_null_checks_in_equality_operators
If you're instead using the YAML map syntax to configure linter rules, add avoid_null_checks_in_equality_operators: true
under linter > rules:
linter:
rules:
avoid_null_checks_in_equality_operators: true
Unless stated otherwise, the documentation on this site reflects Dart 3.6.0. Page last updated on 2025-01-27. View source or report an issue.