Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages
Flutter and app store releases

Flutter rejects a state change made while widgets are building

Your error

setState() or markNeedsBuild() called during build.3 more lines it can show

Fix it fast

Most common cause, 1 of 3

A ChangeNotifier is updated from initState of a widget under the provider

Start the work outside the build.

Fix
ChangeNotifierProvider(create: (_) => OrdersNotifier()..load())

// the call needs a value from the widget:
@override
void initState() {
  super.initState();
  Future.microtask(() => context.read<OrdersNotifier>().load(widget.customerId));
}
Did this fix it?
Full steps for this cause: check, fix, verify

Short answer

setState() or markNeedsBuild() called during build means code running in build() or a child's initState() marked a widget dirty that this pass may already have passed: a ChangeNotifier updated from initState, showDialog called in build, or a parent setState callback. Move the call to an event handler, the provider's create, or a post-frame callback. Applies to Flutter 3.38 to 3.50.x.

  • Applies to Flutter 3.38 to 3.50.x (Dart 3.10 to 3.14), provider 6.1.5+1
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Read the widget type in the line that starts with This and ends in cannot be marked as needing to build. A provider scope points to the first cause, an Overlay or Navigator to the second, and one of your own stateful widgets to the third. The stack trace under it shows which of your methods made the call.

Run this first
flutter run
# in the red exception, find:
#   This <WidgetType> widget cannot be marked as needing to build ...
#   The widget which was currently being built when the offending call was made was ...

Then find the line you see in this list and open its fix:

  1. 1A ChangeNotifier is updated from initState of a widget under the providerThe widget that cannot be marked is provider's _InheritedProviderScope, and the stack passes through notifyListeners() and your initState()
  2. 2showDialog or a route push is called inside build()The widget that cannot be marked is an Overlay, and the dialog call sits in a build method or in a builder callback
  3. 3A child runs its parent's setState callback while it buildsThe widget that cannot be marked is one of your own StatefulWidgets higher up, and the stack shows a child's build() or initState() calling a callback such as onCount or onLoaded

What does the error look like?

  • setState() or markNeedsBuild() called during build.
  • This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets.
  • The widget on which setState() or markNeedsBuild() was called was
  • The widget which was currently being built when the offending call was made was

Cause 1 of 3

A ChangeNotifier is updated from initState of a widget under the provider

You seeThe widget that cannot be marked is provider's _InheritedProviderScope, and the stack passes through notifyListeners() and your initState()

Check

setState() or markNeedsBuild() called during build appears here because the notifier is changed by one of its own descendants while the tree is building. The provider FAQ explains why this is refused: the update is synchronous, so widgets built before the change and widgets built after it would show different values. The usual culprit is a load method that sets a loading flag and calls notifyListeners() at once, started from initState.

Check
@override
void initState() {
  super.initState();
  context.read<OrdersNotifier>().load(); // load() calls notifyListeners() immediately
}

Fix

Start the work outside the build. When it needs no value from the widget, call it in the provider's create or the notifier's constructor. When it does need one, delay the call with Future.microtask so it runs after the current build.

Fix
ChangeNotifierProvider(create: (_) => OrdersNotifier()..load())

// the call needs a value from the widget:
@override
void initState() {
  super.initState();
  Future.microtask(() => context.read<OrdersNotifier>().load(widget.customerId));
}

Verify

The screen opens without the red exception, shows the loading state, then the orders.

Verify
flutter run

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

showDialog or a route push is called inside build()

You seeThe widget that cannot be marked is an Overlay, and the dialog call sits in a build method or in a builder callback

Check

Flutter's common errors guide lists this case: build() is not a place to call setState, directly or indirectly, and showDialog is the indirect kind: it changes the Overlay, which is the widget the error then names. The framework may also run build() on any frame, for instance during an animation, so a dialog opened there would be requested again and again.

Check
grep -rn "showDialog\|showModalBottomSheet\|Navigator.push" lib/

Fix

Open the dialog from the event that calls for it, such as a button press or a finished request. If it must appear as soon as a screen opens, schedule it from initState to run once, after the first frame has been drawn.

Fix
@override
void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_) {
    if (!mounted) return;
    showDialog<void>(context: context, builder: (_) => const WelcomeDialog());
  });
}

Verify

The dialog shows once after the screen renders, and later rebuilds of the screen do not open a second copy.

Verify
flutter run

Confirmed by docs.flutter.dev

Did this fix it?

Cause 3 of 3

A child runs its parent's setState callback while it builds

You seeThe widget that cannot be marked is one of your own StatefulWidgets higher up, and the stack shows a child's build() or initState() calling a callback such as onCount or onLoaded

Check

The framework only lets a widget be marked dirty during the build phase if one of its ancestors is currently building, since a dirty descendant is built later in the same pass anyway. A callback that reaches up and runs setState on the parent marks an ancestor instead, one the framework may already have visited, so the assertion fires.

Check
@override
Widget build(BuildContext context) {
  widget.onCount(items.length); // the parent calls setState in onCount
  return Text('${items.length} items');
}

Fix

Prefer computing the value in the parent and passing it down, which removes the callback. When only the child can know the value, report it after the frame from initState or didUpdateWidget; post-frame callbacks run once and do not request a new frame.

Fix
@override
void initState() {
  super.initState();
  WidgetsBinding.instance.addPostFrameCallback((_) {
    if (mounted) widget.onCount(items.length);
  });
}

Verify

The parent shows the count right after the first frame, and the console stays free of the assertion.

Verify
flutter run

Confirmed by raw.githubusercontent.com

Did this fix it?

How do I stop it coming back?

  • Keep build() free of side effects: no notifyListeners(), setState(), showDialog() or Navigator calls inside it or inside builder callbacks.
  • Give each ChangeNotifier a load method that the provider's create calls, instead of starting loads from widget initState.
  • Guard every post-frame callback that touches context with a mounted check, because the widget may have left the tree by the end of the frame.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine on the same stack: TaxiDex, Qist Bazaar, SessionBird.

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.