The console shows something like this, often when you leave a screen quickly:
setState() called after dispose(): _ProfilePageState#3f2a1(lifecycle state: defunct, not mounted)
This error happens if you call setState() on a State object for a widget that no longer appears in the widget tree ...
Flutter’s own hint, from the framework source, is: “The preferred solution is to cancel the timer or stop listening to the animation in the dispose() callback. Another solution is to check the “mounted” property of this object before calling setState().“
The app usually doesn’t crash. But the message means some code is still running for a screen that no longer exists, and that’s often a memory leak.
Why it happens
dispose() runs when a widget leaves the tree: the user pressed back, a tab switched, or a list item scrolled away. Anything that started earlier and finishes later can still call setState():
- An
await, such as an API call that returns after the user has left the screen. - A
Timerthat keeps firing. - A stream subscription, such as Firestore snapshots, a WebSocket or a location stream, that keeps sending events.
- An animation or listener that was never removed.
Fix 1: check mounted after every await
Future<void> _load() async {
final result = await fetchProfile();
if (!mounted) return; // the page may have closed while we waited
setState(() => name = result);
}
mounted is false once dispose() has run. Put the check after the await, right before you touch setState or context. A check placed before the await doesn’t help.
The same rule applies to using context after an await, such as Navigator, ScaffoldMessenger or Theme. The use_build_context_synchronously lint warns about this. Inside a State class, check mounted:
Future<void> _save() async {
await saveProfile();
if (!mounted) return;
Navigator.of(context).pop();
}
In a StatelessWidget callback or a helper function that only has a BuildContext, check context.mounted instead:
onPressed: () async {
await saveProfile();
if (!context.mounted) return;
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Saved')));
},
Use the matching check. Inside a State class, the lint reports context.mounted as “guarded by an unrelated ‘mounted’ check”.
Fix 2: cancel timers and subscriptions in dispose()
mounted checks hide the symptom, but a timer or stream that keeps running still wastes battery and memory. Cancel them:
Timer? _timer;
StreamSubscription<int>? _sub;
@override
void initState() {
super.initState();
_timer = Timer.periodic(const Duration(seconds: 1), (_) {
setState(() => seconds++);
});
_sub = unreadCount().listen((count) {
setState(() => unread = count);
});
}
@override
void dispose() {
_timer?.cancel();
_sub?.cancel();
super.dispose();
}
Call super.dispose() last, after your own cleanup.
Fix 3: dispose controllers
AnimationController, TextEditingController, ScrollController, PageController and FocusNode all need dispose():
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(seconds: 1),
)..repeat(reverse: true);
@override
void dispose() {
_controller.dispose();
super.dispose();
}
Using Bloc, Riverpod or Provider?
The same error appears when a widget listens to a controller or a ChangeNotifier by hand (addListener) and never calls removeListener. Prefer the package’s own widgets, like BlocBuilder, Consumer or ref.watch. They subscribe and unsubscribe with the widget’s lifecycle, so you never call setState yourself.
Frequently asked questions
What does "setState() called after dispose()" mean?
setState() ran on a State object whose widget has already been removed from the tree, usually because the user left the screen before an await, Timer or stream callback finished. Check mounted after awaits and cancel timers and subscriptions in dispose().
Is it enough to wrap setState in if (mounted)?
For one-off awaits, yes. For Timers, streams and listeners, also cancel them in dispose(); otherwise they keep running in the background and leak memory, even though the error disappears.
What is the difference between mounted and context.mounted?
Use mounted inside a State class and context.mounted where you only have a BuildContext, such as a StatelessWidget callback or a helper function. The use_build_context_synchronously lint expects that pairing, and inside a State it reports context.mounted as an unrelated check.
Can this error crash a release build?
It is reported as an error but usually does not crash the app. It does mean code is still running for a closed screen, which can cause memory leaks and wasted network or battery use, so fix the cause.
Stuck on a build or a store release? I fix and maintain Flutter apps, or tell me about your project. More fixes: all Flutter answers.