如果我必须预处理数据,Flutter FlushBar 不起作用

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

我正在尝试在 Flutter 中构建一个表单。用户输入一个值并单击一个按钮,我 运行 对该值进行一些基本验证,然后显示一个 AlertDialogue 作为确认。如果用户点击确认我想尝试一个 API 调用,获取结果并显示那个 API 调用的结果让用户知道什么 happened.If 我显示一个带有硬编码的 Flushbar重视它的工作原理。但是,如果我首先尝试对对象进行一些字符串操作,则不会显示 Flushbar。如果我尝试将函数的响应直接打印到 Flushbar 中,那也不起作用。 关于为什么首先会出现此问题以及解决此问题的最佳方法的任何建议?

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

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

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

                 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));
                        },