prefer_conditional_expressions
This rule flags an if/else that does nothing but assign or return two values.
Six lines to choose between two strings buries the one thing that varies. A conditional expression puts the choice and both outcomes on one line, and makes it obvious that every path assigns exactly once — which the block form only implies.
Both branches assign the same target:
String describe({required bool isActive}) { String label; if (isActive) { label = 'On'; } else { label = 'Off'; } return label;}Both branches return:
String greeting({required bool isMorning}) { if (isMorning) { return 'Good morning'; } else { return 'Good evening'; }}String describe({required bool isActive}) { final label = isActive ? 'On' : 'Off'; return label;}
String greeting({required bool isMorning}) => isMorning ? 'Good morning' : 'Good evening';Known limitations
Section titled “Known limitations”Only the two shapes where the rewrite is exact are reported. These are all left alone, because collapsing them would change what the code does:
String describe({required bool isActive}) { String label; String tone;
// Two statements in a branch. if (isActive) { label = 'On'; tone = 'green'; } else { label = 'Off'; tone = 'grey'; }
// Different targets. if (isActive) { label = 'On'; } else { tone = 'grey'; }
// Different operators — `=` in one branch, `+=` in the other. if (isActive) { label = 'On'; } else { label += 'Off'; }
// No `else`. if (isActive) { label = 'On'; }
return '$label$tone';}An else if chain is a sequence of tests rather than a two-way choice, and a
pattern if (if (x case ...)) binds variables a conditional expression
cannot — both are skipped.
Configuration
Section titled “Configuration”This rule is in the pedantic preset. Enable it by name:
rules: prefer_conditional_expressions: trueTo turn it off again:
rules: prefer_conditional_expressions: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_nested_conditional_expressions— Flag a conditional nested inside another.prefer_switch_expression— Suggest converting switch statements to switch expressions.prefer_switch_with_enums— Use a switch instead of an if-else chain over enum constants.avoid_contradictory_expressions— Detect logical AND conditions that always evaluate to false.