Skip to content

Flutter errorFlutter: “setState() or markNeedsBuild() called during build”

The app throws setState() or markNeedsBuild() called during build, often while opening a screen. Something changes state while Flutter is still building widgets: a setState in build, a showDialog or Navigator call in build, or a provider or controller that notifies listeners during build.

Short answer

Move the state change out of build. Show dialogs and navigate from an event handler, such as onPressed, or after the first frame with WidgetsBinding.instance.addPostFrameCallback in initState. If a ChangeNotifier calls notifyListeners during build, trigger that update from initState or an event instead.

Why the state change is rejected, and the fix

  1. 01

    setState is called inside build

    “The build method in your Flutter code isn't a good place to call setState, either directly or indirectly.” Flutter, Common Flutter errors

    build can run many times a second, for example during an animation. Changing state there asks Flutter to rebuild while it is already building. Calculate values in build without saving them, or change state in an event handler or lifecycle method.

  2. 02

    A dialog or navigation is triggered from build

    “The build method isn't the right place to call showDialog because build can be called by the framework for every frame, for example, during an animation.” Flutter, Common Flutter errors

    showDialog, Navigator.push and snackbars change the widget tree, so calling them from build hits the same error even without a visible setState. Trigger them from the button that opens the screen, or after the first frame.

    // in a StatefulWidget's State
    @override
    void initState() {
      super.initState();
      WidgetsBinding.instance.addPostFrameCallback((_) {
        if (!mounted) return;
        showDialog<void>(
          context: context,
          builder: (_) => const AlertDialog(title: Text('Welcome')),
        );
      });
    }
  3. 03

    Open the dialog as a route instead

    “One way to avoid this error is to use the Navigator API to trigger the dialog as a route.” Flutter, Common Flutter errors

    When a screen should open with a dialog on top, push both from the button that opens it: first the screen, then the dialog as its own route. Flutter’s docs show this pattern with Navigator and PageRouteBuilder.

  4. 04

    A ChangeNotifier notifies during build

    With Provider and a ChangeNotifier, the same error appears when build calls a method that changes the model and calls notifyListeners, such as starting a data load. Start the load in initState (after the first frame if it notifies right away) or in the model itself, and only read state in build.

Checked against the vendor’s docs on : Flutter docs: Common Flutter errors (setState called during build).

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: Flutter “A RenderFlex overflowed” · Flutter “RenderBox was not laid out” · Flutter MissingPluginException · Flutter requires a newer Kotlin Gradle plugin

Questions

More on this error

Is addPostFrameCallback a hack?

It is the standard way to run code once the current frame has finished, such as showing a dialog when a screen opens. Check mounted inside the callback, in case the user left the screen first.

Why does it only happen sometimes?

build runs whenever Flutter decides to rebuild: on navigation, a theme change, the keyboard opening or an animation. A state change in build fails on the rebuilds that happen while another widget is still building.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.