avoid_cascade_after_if_null
v0.3.0 Warning Fix Control Flow
Warns when a cascade expression (..) follows an if-null (??) operator without parentheses. The precedence is not what it looks like: the cascade applies to the entire if-null expression, not to the right-hand side of ??.
Why use this rule
Section titled “Why use this rule”The cascade operator binds looser than ??, so a ?? B()..method() parses as (a ?? B())..method(). The cascade takes the whole if-null expression as its target.
That is the opposite of how the line reads. The natural reading is “if a is null, build a B and configure it” — but when a is non-null, the cascade runs against a itself:
final sb = StringBuffer('LHS');StringBuffer? maybe = sb;final out = maybe ?? StringBuffer('RHS')..write('-MUTATED');// sb is now "LHS-MUTATED": the pre-existing buffer was mutated,// the fresh StringBuffer('RHS') was discarded untouched,// and `out` is identical to `sb`.Adding explicit parentheses makes the intent clear and prevents subtle bugs.
See also: Cascade notation
void bad(Kettle? spareKettle) { // Unclear whether ..boil() applies to the result of ?? or just Kettle() final kettle = spareKettle ?? Kettle() ..boil();
// Multiple cascades after if-null final kettle2 = spareKettle ?? Kettle() ..boil() ..litres = 5;}void good(Kettle? spareKettle) { // Cascade applies to the entire if-null expression final kettle = (spareKettle ?? Kettle())..boil();
// Cascade applies only to the new instance final kettle2 = spareKettle ?? (Kettle()..boil());
// No if-null involved, cascade is unambiguous final kettle3 = Kettle()..boil();}Configuration
Section titled “Configuration”This rule is in the recommended preset, so it is on with
preset: recommended or preset: opinionated. Add it to preset: core with
avoid_cascade_after_if_null: true.
To turn it off:
rules: avoid_cascade_after_if_null: falseTo keep the rule on but skip certain paths, use per-rule exclude.
Related rules
Section titled “Related rules”avoid_unused_after_null_check— A variable null-checked but never used in the guarded branch.prefer_simpler_patterns_null_check— Suggest simpler null-check patterns in if-case expressions.avoid_collapsible_if— Merge nested if statements with &&.avoid_constant_conditions— Detect comparisons where both sides are constants.