setState() called after dispose()

setState() called after dispose(): _MyWidgetState#abcde(lifecycle state: defunct, not mounted)

Quick Answer

You called setState on a widget that has already been removed from the widget tree.

Why This Happens

In Flutter, this happens when an async operation completes after the widget has been disposed, and the callback tries to call setState. The widget is no longer mounted, so updating its state is invalid. You should check the mounted property before calling setState in async callbacks.

The Problem

class _MyWidgetState extends State<MyWidget> {
  void fetchData() async {
    final data = await api.getData();
    setState(() {
      _data = data;
    });
  }
}

The Fix

class _MyWidgetState extends State<MyWidget> {
  void fetchData() async {
    final data = await api.getData();
    if (mounted) {
      setState(() {
        _data = data;
      });
    }
  }
}

Step-by-Step Fix

  1. 1

    Identify the error

    Look at the error message: setState() called after dispose(). This means a widget tried to update after being removed from the tree.

  2. 2

    Find the cause

    Locate async operations (Future, Timer, Stream) in the widget that call setState in their callbacks without checking if the widget is still mounted.

  3. 3

    Apply the fix

    Add an if (mounted) check before every setState call in async callbacks, or cancel timers and subscriptions in dispose().

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free