0

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'),
          ),
        ),
4

1 回答 1

2

我认为问题在于您正在尝试立即从 API 调用访问数据,但是必须等待通过 API 调用的数据。并且最好在显示同花栏后弹出。

所以如果bb.trade(_character, finalValue, true);返回未来你应该这样做

                 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));
                        },
于 2021-04-14T09:46:47.647 回答