将Future作为参数传递给另一个类

问题描述 投票:0回答:1

是否有可能将Future传递给对话框帮助程序类,当用户加载失败,并且当用户单击重试时,我试图显示一个对话框,该应用将再次启动网络调用

将来传递给对话框类

void getCountryApi() async {

    GlobalService globalService = new GlobalService();

    ...

    //wait for future to be finished
    bool isSuccessful =
        await globalService.requestApi(CountryApi, queryParameters, context);
    if (isSuccessful) {
      dialogManager.information2(context, 'Success', globalService.fullData);
    } else {
      //dialogManager.information2(context, 'Success', globalService.fullData);
      dialogManager.retryDialog(context, 'Failed', globalService.fullData,globalService.requestApi(CountryApi, queryParameters, context));
    }
  }

对话助手类

retryDialog(BuildContext context, String title, String description, Future apiCall) {
    return showDialog(
        context: context,
        barrierDismissible: false,
        builder: (BuildContext context) {
          return WillPopScope(
            onWillPop: () {},
            child: AlertDialog(
              title: Text(title),
              content: Column(
                mainAxisSize: MainAxisSize.min,
                children: <Widget>[
                  Text(description),
                  Row(
                    mainAxisSize: MainAxisSize.min,
                    children: <Widget>[
                      SizedBox(height: 30),
                      Padding(
                        padding: const EdgeInsets.all(8.0),
                        child: RaisedButton(
                          shape: new RoundedRectangleBorder(
                            borderRadius: new BorderRadius.circular(30.0),
                          ),
                          color: Colors.red,
                          onPressed: () async {
                            Navigator.pop(context);
                            await apiCall;
                          },
                          child: Text(
                            'Retry',
                            style: TextStyle(color: Colors.white),
                          ),
                        ),
                      ),                    
                    ],
                  ),
                ],
              ),
            ),
          );
        });
  }

我面临的问题是,将来的对象将在此行立即执行网络调用

dialogManager.retryDialog(context, 'Failed', globalService.fullData,globalService.requestApi(CountryApi, queryParameters, context));

在用户单击重试按钮之前

flutter dart
1个回答
0
投票

您的方法不正确,成功或失败时,您只需要显示警报对话框,并在警报按钮上提到所有API调用即可。

© www.soinside.com 2019 - 2024. All rights reserved.