no_literal_bool_comparisons
Don't compare boolean expressions to boolean literals.
Details
#From Effective Dart:
DON'T use true
or false
in equality operations.
This lint applies only if the expression is of a non-nullable bool
type.
BAD:
dart
if (someBool == true) {
print('true!');
}
while (someBool == false) {
print('still false!');
}
GOOD:
dart
if (someBool) {
print('true!');
}
while (!someBool) {
print('still false!');
}
Enable
#To enable the no_literal_bool_comparisons
rule, add no_literal_bool_comparisons
under linter > rules in your analysis_options.yaml
file:
analysis_options.yaml
yaml
linter:
rules:
- no_literal_bool_comparisons
If you're instead using the YAML map syntax to configure linter rules, add no_literal_bool_comparisons: true
under linter > rules:
analysis_options.yaml
yaml
linter:
rules:
no_literal_bool_comparisons: 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.