Search code examples
flutterflutter-layoutflutter-android

Flutter FlushBar does not work if I have to pre-process data


I am trying to build a form in Flutter. A user enters a value and clicks on a button, I run some basic validation on that value then show an AlertDialogue as a confirmation. If the user clicks on confirm I want to attempt an API call, get the result and display the result of that API call to let the user know what happened.If I display a Flushbar with hardcoded values it works. But If I try to do some string manipulation on the object first the Flushbar does not display. If I try to print the response from the function right into the Flushbar that also does not work. Any advice on why this problem is occurring in the first place, and what would be the best way to solve it?

        Padding(
          padding: const EdgeInsets.symmetric(vertical: 15.0),
          child: ElevatedButton(
            style: ElevatedButton.styleFrom(
              textStyle: TextStyle(
                fontSize: 30,
              ),
              primary: Colors.lightGreen, // background
              onPrimary: Colors.white, // foreground
            ),
            onPressed: () {
              // Validate returns true if the form is valid, or false otherwise.
              if (_formKey.currentState.validate())
              {
                  return showDialog<void>(
                    context: context,
                    barrierDismissible: false, // user must tap button!
                    builder: (BuildContext context) {
                      return AlertDialog(
                        title: Text('Confirmation'),
                        content: SingleChildScrollView(
                          child: ListBody(
                            children: <Widget>[
                              Text('Please confirm your action'),
                              Text('Would you like to buy ' + finalValue.toString() + ' worth of merchandise'),
                            ],
                          ),
                        ),
                        actions: <Widget>[
                          TextButton(
                            child: Text('No'),
                            onPressed: () {
                              Navigator.of(context).pop();
                            },
                          ),
                          TextButton(
                            child: Text('Confirm'),
                            onPressed: ()  {
                              Navigator.of(context).pop();
                              var response = bb.trade(_character, finalValue, true); //API CALL
                              *//String resp = response.body.split(',')[1].split(':')[1];
                              //resp = resp.substring(1);
                              //resp = resp.split('.')[0];
                              //print(resp);* //The real string manipulation we want to do but then flushbar does not display
                              Flushbar(
                                title:  "Result",
                                message:  "lol"+response.body.toString(), //FLUSHBAR DOES NOT DISPLAY AT ALL
                                duration:  Duration(seconds: 5),
                              )..show(context);
                            },
                          ),
                        ],
                      );
                    },
                  );
              }
            },
            child: Text('Buy'),
          ),
        ),

Solution

  • I Think the problem is that you are trying to access data from API call immediately , however data coming over API call must be awaited for. and it is preferred to pop after showing the flush bar .

    so if bb.trade(_character, finalValue, true); return future you should do like this

                     onPressed: () async {
                        
                              var response = await bb.trade(_character, finalValue, true);
    
                              Flushbar(
                                title:  "Result",
                                message:  "lol"+response.body.toString(), 
                                duration:  Duration(seconds: 5),
                              )..show(context).then((value) => Navigator.pop(context));
                            },