Flutter rejects a state change made while widgets are building
Fix it fast
Most common cause, 1 of 3A ChangeNotifier is updated from initState of a widget under the provider
Start the work outside the build.
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));
}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.
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.
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:
- 1A ChangeNotifier is updated from initState of a widget under the provider
The widget that cannot be marked is provider's _InheritedProviderScope, and the stack passes through notifyListeners() and your initState() - 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 - 3A child runs its parent's setState callback while it builds
The 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 wasThe 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.
@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.
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.
flutter runConfirmed by raw.githubusercontent.com
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.
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.
@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.
flutter runConfirmed by docs.flutter.dev
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.
@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.
@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.
flutter runConfirmed by raw.githubusercontent.com
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.