【问题标题】:Passing Future as parameter to another class将 Future 作为参数传递给另一个类
【发布时间】:2020-03-14 14:04:59
【问题描述】:

是否可以将 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),
                          ),
                        ),
                      ),                    
                    ],
                  ),
                ],
              ),
            ),
          );
        });
  }

我面临的问题是,future 对象在这一行立即执行网络调用

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

在用户点击重试按钮之前

【问题讨论】:

  • 您可以像传递任何其他数据类型一样传递 Future。如果您想要更具体的答案,您将必须更具体。
  • 嗨@Abion47,感谢您的回复,我目前的方法的问题是,每当我将未来传递给对话框类时,未来就会在调用 onpress 之前立即运行
  • 不清楚你指的是什么 Future 以及你将它传递给对话框类的位置。
  • 我试图传入 Future 'await globalService.requestApi',它是一个未来,我尝试将它作为一个函数传递给' dialogManager.retryDialog' 帮助器类,重试对话框接受 3 个参数我正在尝试将 Future/Function 作为参数之一传递,但它不起作用,因为它是一个异步函数,将它作为 Future 传递会导致它在对话框渲染之前立即启动。
  • globalService.requestApi 是未来。通过awaiting 它,您选择等待未来完成并存储结果(在这种情况下是bool)。如果您想使用未来本身,请删除await。

标签: flutter dart


【解决方案1】:

您的方法不正确,无论成功还是失败,您只需要显示警报对话框,并且在警报按钮上提及所有 API 调用。

【讨论】:

  • 是的,现在我试图将 API 调用传递到对话框的 onpress 按钮中,但我不确定我应该如何传递它。我尝试将它作为函数传递,但它不起作用,因为它是一个异步函数,将它作为 Future 传递会导致它在对话框渲染之前立即启动。
  • 您可以使用此 [链接] (stackoverflow.com/questions/49135632/…) 从 UI 调用异步函数。
  • 我明白了,我正在尝试构建一个通用对话框助手来处理我的网络成功/失败,但我会尝试使用流构建器,谢谢!
猜你喜欢
  • 2013-10-13
  • 2012-10-18
  • 2023-01-18
  • 1970-01-01
  • 1970-01-01
  • 2021-01-19
  • 1970-01-01
  • 1970-01-01
  • 2017-08-07
相关资源
最近更新 更多