Skip to content

Flutter error

Flutter: “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.

Rather hand it over? App Fix Ticket: One reproducible bug in an existing mobile or web app, diagnosed and fixed.The Fix is $1,200 in total. The $450 Diagnosis is optional and credited toward it.

Full refund if Simon can’t take it on once he sees your project, or if you cancel before work starts. Refund terms.

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.

Rather not fix it yourself? App Fix Ticket fixes one reproducible bug at a fixed price, with an optional Diagnosis that is credited toward the fix.

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.

“Very responsive, attentive to fixing bugs, and completed the project as needed! Thank you!”
A buyer in the United States, July 2022. Shopify on Fiverr
Read 126 of the 127 Fiverr reviews

Related: Flutter “A RenderFlex overflowed” · Flutter “RenderBox was not laid out” · Flutter MissingPluginException · Flutter “setState() called after dispose()”

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.

I just added the field and it fails at once. Why?

Hot reload keeps the State objects already on screen and does not run initState again, so the new field was never set. Do a hot restart before debugging further. Flutter’s hot reload page: “it doesn’t rerun main() or initState()”.

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. If you already know the cause, you can pay for the fix directly; if not, start with a written diagnosis, which is credited toward 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?

Ask Simon for a price range

Send the error and what you were doing. Simon replies by email, usually within one business day, with a price range.