Dart type promotion is based on a check of a variable going a particular way dominating a later use of that variable.
So, if you do if (x != null) x.foo();
, it detects that the check x != null
being true means that the later x.foo()
is valid. That only works because the compiler can also convince itself that the variable's value doesn't change between the check and the use.
If you introduce an extra boolean variable, like here, then the check is no longer performed inside the branch. That doesn't make it impossible to remember that the isNonNull
boolean value being true means that foo
is non-null, but it gets more complicated. The compiler now has to ensure that foo
doesn't change and that isNotNull
doesn't change.
The Dart compiler bails out instead, it doesn't track that level of complication. It only tracks promotion through one variable at a time.