0
votes

I want show AlertDialog if API call return error.

But when I try show AlertDialog on API call fail I get error:

E/flutter (25472): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: setState() or markNeedsBuild() called during build. E/flutter (25472): This Overlay widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase. E/flutter (25472): The widget on which setState() or markNeedsBuild() was called was: E/flutter (25472):
Overlay-[LabeledGlobalKey#faad1]

Maybe is because I am try show AlertDialog while AnimatedContainer parent is animating (it animate on error):

 AnimatedContainer(
 …
 onPressed() async { 
 callAPI();

 if (error) {
    
await  _showAlertDialog();
 }

For AlertDialog I am use standard Flutter example:

Future<void> _showAlertDialog() async {
  return showDialog<void>(
    context: context,
    barrierDismissible: false, // user must tap button!
    builder: (BuildContext context) {
      return AlertDialog(
        title: Text('Rewind and remember'),
        content: SingleChildScrollView(
          child: ListBody(
            children: <Widget>[
              Text('You will never be satisfied.'),
              Text('You\’re like me. I’m never satisfied.'),
            ],
          ),
        ),
        actions: <Widget>[
          FlatButton(
            child: Text('Regret'),
            onPressed: () {
              Navigator.of(context).pop();
            },
          ),
        ],
      );
    },
  );
}

How I can show AlertDialog while parent AnimatedContainer is animating?

Thanks!

1

1 Answers

0
votes

The error reminds us that we have to build the page before we can create this new component.

The solution may be use addPostFrameCallback method, waiting for page build to complete after requesting data:

WidgetsBinding.instance.addPostFrameCallback((_){
      /// Interface Request
    });

You can find more information on this website: https://www.didierboelens.com/faq/week2/