What this thread does
Do first: cancel the callback's timer/subscription in dispose() and check mounted immediately before every post-await setState().
Short answer
As of November 2026, Flutter 3.50.0 uses the documented State lifecycle: disposal is terminal, and later state updates are invalid (dispose).
Diagnostic: setState() called after dispose()
Trace the callback that still holds the disposed State. MorinaDev's August 16, 2023 notification report records setState() called after dispose() (source); it is not evidence that every such failure is a Flutter regression.
Choose / don't choose
- Cancel owned producers: choose for timers and listeners; don't keep producing widget-only results after disposal (setState).
- Discard a late result: choose as a second guard when cancellation is unavailable or an operation has already completed.
- Long-lived work: give it a non-widget owner if it must outlive navigation; don't retain a disposed State to deliver its results.
Not for: provider and router selection; see riverpod-bloc-provider-signals-and-routing-for-a-flutter-app.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
State.mounted | Proceed only when true; evaluate again immediately after each async gap | Current Flutter lifecycle | mounted |
setState callback | Synchronous state mutation only; cannot be async or return a Future | Current Flutter API | setState |
Timer.cancel() | Call for every owned timer during disposal; repeated cancellation is allowed | Current Dart timer API | cancel |
StreamSubscription.cancel() | Call during disposal to stop events; returned Future represents source cleanup | Current Dart stream API | cancel |
dispose() / super.dispose() | Unsubscribe/release resources, then end override with super.dispose(); lifecycle method returns void | Current Flutter API | dispose |
Order of operations
1. Subscribe in initState; if the source changes in didUpdateWidget, detach the old source before attaching the new one. Retain cancellation/listener handles (dispose). 2. Await outside the setState callback; then check lifetime at the point of mutation. Example application-defined load:
final result = await load();
if (!mounted) return;
setState(() { data = result; });
Repeat the guard after each further await, including error/finally paths that update state (setState, mounted). 3. In dispose, stop timers, cancel stream subscriptions, remove listeners and dispose owned controllers before super.dispose(). Timer cancellation prevents its future callbacks (timer); stream cancellation prevents new events and returns asynchronous cleanup (stream). 4. dispose() returns void. If later resource deletion depends on completed stream cleanup, arrange that sequencing in a separate owner that can await the cancellation Future (dispose, stream). 5. If failure persists, inspect the stack for another timer/listener, a then callback, or an error/finally branch without a post-gap check. Navigate away while loading and complete both success and failure paths.
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
setState() called after dispose() | Another object retains a reference to this State object after removal | Cancel its owner; guard the mutation after awaits | setState, morinaDev report |
setState() callback argument returned a Future. | The setState() callback returned a Future | Await first; check mounted; perform synchronous mutation | setState implementation |
Missing
- The linked source does not confirm: a mounted check before an await says nothing about whether the State survives that await; canceling a subscription does not retract an already-running async callback; mounted alone cannot stop an older response replacing newer data. The request library's cancellation API is unspecified.
Checked
2026-11-12; stable release notes, Flutter State references, Dart cancellation APIs and dated notification report read. No Flutter commands executed.
Started from a post by @Flutter, packaged by the editor. Original post