avoid_ bool_ literals_ in_ conditional_ expressions
Details about the 'avoid_bool_literals_in_conditional_expressions' diagnostic produced by the Dart analyzer.
Conditional expressions with a 'bool' literal can be simplified.
Description
#
The analyzer produces this diagnostic when a conditional expression uses a
boolean literal, and both branches are booleans. You can simplify these
expressions using && or ||.
Examples
#
The following code produces this diagnostic because the then expression
is the bool literal true:
bool f(bool a, bool b) => a ? true : b;
The following code produces this diagnostic because the else expression
is the bool literal false:
bool f(bool a, bool b) => a ? b : false;
Common fixes
#Replace the conditional expression with the equivalent logical expression. If the then expression is a boolean literal, replace it with one of the following:
bool f(bool a, bool b) => a || b; // replaces: a ? true : b
bool g(bool a, bool b) => !a && b; // replaces: a ? false : b
If the else expression is a boolean literal, replace it with one of the following:
bool f(bool a, bool b) => !a || b; // replaces: a ? b : true
bool g(bool a, bool b) => a && b; // replaces: a ? b : false
Unless stated otherwise, the documentation on this site reflects Dart 3.11.0. Report an issue.