Symptom
A timer, stream listener or asynchronous completion attempts to call setState after its widget has left the tree. The operation outlived the state owner.
Find the resource owner
Search for the timer, subscription or request started by that feature. A repeated listener created in build can also leave multiple active callbacks. Pair resource creation with cleanup in the owner that created it.
Fix lifecycle, not only the exception
Cancel owned timers and subscriptions in dispose. For an asynchronous result that cannot be canceled, verify mounted before updating widget-local state after the wait. Also decide whether the work belongs in a longer-lived repository or feature model.
Avoid using mounted as a universal patch
A mounted check can prevent the invalid update while leaving unnecessary work running. It is not a substitute for canceling resources or preventing stale responses from updating a new query.
Apply the pattern to a real example
The focus timer project cancels its timer in dispose and derives remaining time from a deadline. The lifecycle guide covers controller ownership.
Worked example
These commands run from a local project terminal.
# Reproduce in a local app and inspect the first relevant stack frame:
flutter run
Read the result
Apply only the change that matches the observed cause. Keep the original diagnostic output so you can compare the same command after the change.
Remove the stale callback, not only the visible exception
Cancel owned timers and subscriptions when the feature ends. For work that cannot be canceled, check whether its widget owner is still mounted before a widget-local update. A query-based feature also needs to reject results from an older request even while the widget remains mounted.
Reproduce by leaving the screen immediately after starting the operation. Repeat the flow and look for accumulating work. The memory game demonstrates canceling an old round’s timer, and async state introduces the visible request states.
Practice and verification
Start the work, immediately navigate away and wait beyond its original completion time. Repeat the flow several times. Verify there is no disposed-state exception and no accumulating background timer or subscription.