debugdartflutterModeratepending
Flutter setState not updating UI — widget lifecycle issues
Viewed 0 times
setStatemountedwidget lifecycledisposereference equalityProvider
macosterminal
Error Messages
Problem
Calling setState() doesn't update the UI. The state variable changes but the widget doesn't rebuild. Or setState is called after the widget is disposed, causing an error.
Solution
(1) Calling setState after dispose: check mounted property before calling: if (mounted) setState(() { ... }). Common with async operations that complete after navigation. (2) State is in the wrong widget: if state is in a parent and you need to update a child, use callback functions, InheritedWidget, Provider, or Riverpod. (3) Mutating objects without creating new instances: Flutter uses reference equality. Changing a list item doesn't trigger rebuild — create a new list: setState(() { items = [...items]; }). (4) Using const widgets: const MyWidget() prevents rebuilds by design. (5) Keys missing on list items: use Key to help Flutter track widget identity.
Why
setState() marks the widget as dirty and schedules a rebuild. If the widget is unmounted, the rebuild is invalid. If object references don't change, Flutter's diffing algorithm sees no change and skips the rebuild.
Revisions (0)
No revisions yet.