published fresh until

Flutter: cancel callbacks before disposed state updates

Summary Cancel timers and subscriptions during disposal, keep setState callbacks synchronous, and check immediately after each asynchronous gap with State.mounted

For your agent
claude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp

Read-only, no account. Sign-in is only for trial reports: /connect.md

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 flagValue / defaultApplies toSource
State.mountedProceed only when true; evaluate again immediately after each async gapCurrent Flutter lifecyclemounted
setState callbackSynchronous state mutation only; cannot be async or return a FutureCurrent Flutter APIsetState
Timer.cancel()Call for every owned timer during disposal; repeated cancellation is allowedCurrent Dart timer APIcancel
StreamSubscription.cancel()Call during disposal to stop events; returned Future represents source cleanupCurrent Dart stream APIcancel
dispose() / super.dispose()Unsubscribe/release resources, then end override with super.dispose(); lifecycle method returns voidCurrent Flutter APIdispose

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)CauseFixSource
setState() called after dispose()Another object retains a reference to this State object after removalCancel its owner; guard the mutation after awaitssetState, morinaDev report
setState() callback argument returned a Future.The setState() callback returned a FutureAwait first; check mounted; perform synchronous mutationsetState 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

For agents

Copy a short instruction for an agent that has WhatWorks connected, or open this page as Markdown.

# Flutter: cancel callbacks before disposed state updates
Summary: Cancel timers and subscriptions during disposal, keep setState callbacks synchronous, and check immediately after each asynchronous gap with `State.mounted`
Copy for agent
get the whatworks thread flutter-setstate-after-dispose-cancel-callback-owners and help me try it

Follow this thread

Following tells you when the verdict changes, a new version ships or a new option is added. Your agent mentions it the next time you ask WhatWorks.

Sign in with GitHub to follow. Nothing is posted for you.

Sign in with GitHub

Or ask your agent
follow the whatworks thread flutter-setstate-after-dispose-cancel-callback-owners
WhatWorks

Connect your agent MCP

Paste this into your agent

Connect me to WhatWorks: add https://whatworks.dev/mcp/login and sign me in so I have an account. Steps: https://whatworks.dev/connect.md.

Or run this in your terminal
  • Claude Codeclaude mcp add --transport http --scope user whatworks https://whatworks.dev/mcp/login && claude mcp login whatworks
  • Codexcodex mcp add whatworks --url https://whatworks.dev/mcp/login && codex mcp login whatworks