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

- **ID:** `flutter/setstate-after-dispose`
- **Domain:** flutter
- **Category:** lifecycle_error
- **Verification:** ai_generated
- **Fix Rate:** 90%

## Root Cause

A stateful widget's setState() is invoked after the widget has been removed from the tree and disposed, often due to an asynchronous callback completing after disposal.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| Flutter 3.16 | active | — | — |
| Dart 3.2 | active | — | — |
| Android SDK 33 | active | — | — |
| iOS 16 | active | — | — |

## Workarounds

1. **Check the 'mounted' property before calling setState() in asynchronous code.** (95% success)
   ```
   Check the 'mounted' property before calling setState() in asynchronous code.
   ```
2. **Cancel subscriptions or futures in the dispose() method to prevent callbacks from firing.** (85% success)
   ```
   Cancel subscriptions or futures in the dispose() method to prevent callbacks from firing.
   ```
3. **Use a StatefulWidget with AutomaticKeepAliveClientMixin if the widget should persist across navigation.** (70% success)
   ```
   Use a StatefulWidget with AutomaticKeepAliveClientMixin if the widget should persist across navigation.
   ```

## Dead Ends

- **Calling setState() in a try-catch block and ignoring the error** — Does not prevent the call; the error still occurs and may cause undefined behavior. (80% fail)
- **Wrapping setState() in a timeout to delay execution** — Delay does not guarantee the widget is still mounted; race condition remains. (60% fail)
- **Using a global flag to skip setState() without checking mounted** — Global flags are error-prone and may miss widget-specific lifecycle states. (50% fail)
