Open navaronbracke opened 9 months ago
CC @goderbauer
Nice suggestion. I think this would be a good one to have. setState
and using context
after an async gap should follow the same rules. I wonder how many violations of this we have in the framework...
@stereotype441 (re: flow analysis)
use_set_state_synchronously
I propose a new lint, akin to its counterpart,
use_build_context_synchronously
, but for setState() invocations.Description
Use setState() synchronously and guard it with a mounted check if needed.
Details
Using setState() after an async gap is potentially unsafe. Consider guarding it with a mounted check.
We should consider both explicit
await
s and sync/async versions of the then/catchError/whenComplete callbacks.Kind
It guards against errors.
Bad Examples
Good Examples
Discussion
I have seen bugs (both in the framework and in user code) due to bad usages of
setState()
after an async gap.I.e. https://github.com/flutter/flutter/issues/124597
Discussion checklist