Skip to main content

avoid-cascade-after-if-null

added in: 1.6.0

Warns when a cascade expression is used after if null (??) binary expression without parentheses.

Not adding parentheses may lead to unexpected results since cascade will be executed after if null expression.

Additional resources:

Example

❌ Bad:

class Cow {
void moo() {}
}

class Ranch {
final Cow? _cow;

Ranch([Cow? cow])
: _cow = cow ?? Cow()
..moo(); // LINT
}

void main() {
final Cow? nullableCow;

final cow = nullableCow ?? Cow()
..moo(); // LINT
}

✅ Good:

void main() {
final Cow? nullableCow;

final cow = (nullableCow ?? Cow())..moo();
final cow = nullableCow ?? (Cow()..moo());
}