Open yyoon opened 7 years ago
I usually have this problem if I'm passing a function/closure to the thing I'm cascading that references the variable being set due to the cascade:
For instance:
Object _foo;
void bar() {
_foo = new Foo()..addListener(_useFoo);
}
void _useFoo() {
_foo.use();
}
In the above, if Foo.addListener calls the function _useFoo then _foo is unexpectedly null.
Doing this is more right in this case:
Object _foo;
void bar() {
_foo = new Foo();
_foo.addListener(_useFoo);
}
void _useFoo() {
_foo.use();
}
I don't think we could meaningfully catch these cases without overcatching by a lot. Tricky situation.
On second thought, I think we could catch when the cascade is part of an assignment, and the assignee is a field. (It's valid for when the assignee is a variable too, but less common?)
This is related to the
cascade_invocations
rule.We recently did a massive refactoring to address the
cascade_invocations
warnings, and found that one of those changes introduced a bug in our code.The
doSomething()
call changes some internal states and then notifies all the listeners, and that includes the_handleEvent()
method. This_handleEvent()
implementation then reads the internal state ofBar
, so it is important that_bar
is correctly assigned before this happens.We changed this to use cascading style instead.
and it started to crash, because
_bar
is not yet assigned when_handleEvent()
is called for the first time due to thedoSomething()
call.We fixed this on our side once we found the issue, but it wasn't an obvious problem to find. I would think it would be difficult for the linter to determine whether it is safe to use cascade or not, but just wanted to call this out as a problem. Also related to #787, as it's also about the order between the assignment and the method calls on that object.