美文网首页flutter
flutter showDialog回调的问题

flutter showDialog回调的问题

作者: 天才一般的幼稚 | 来源:发表于2019-11-07 17:58 被阅读0次

    一、问题描述

    点击方法触发弹框,但是点击确定后回调函数一直接收不到return的值。

    调用方法

    _submit(this.userId, this.roomId, this.uuid, this.mac, "0").then((val){
          if(val == "0"){ //此处接不到值
            逻辑代码处理
          );
    
     });
    

    方法体

    Future _submit(String userId, String roomId, String uuid, String mac, String isReplace) async {
        Map resultMap;
        try {
          //HTTP请求
        } catch (e) {
          print('请求异常' + e.toString());
        }
        if(resultMap["code"] == "0"){
          return "0"; // 此处返回可以接到
        } else {
          showDialog(
              context: context,
              builder: (context) {
                return AlertDialog(
                  content: new SingleChildScrollView(
                    child: ListBody(
                      children: <Widget>[Text("提示文字"), Text("")],
                    ),
                  ),
                  actions: <Widget>[
                    FlatButton(
                      child: Text("确定"),
                      onPressed: () async {
                        Map resultMap1;
                        try {
                          //HTTP请求
                        } catch (e) {
                          print('请求异常' + e.toString());
                        }
                        if(resultMap1["code"] == "0"){
                          return "1"; //此处无法接到
                        } else {
                            //处理失败提示
                          )
                          );
                        }
                      }
                    ),
                    FlatButton(
                      child: Text("取消"),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    )
                  ],
                );
              });
        }
      }
    

    二、原因

    看了一些介绍之后发现,其实原因很简单。因为dialog其实是另一个页面,准确地来说是另一个路由,因为dialog的关闭也是通过navigator来pop的,所以它的地位跟你当前主页面一样。也就是说,此时,在showDialog内部直接返回在调用函数的回调方法是接收不到的,因为已经不在同一个页面里了。

    三、解决方案

    需要传递回去就需要使用Navigator.pop将参数返回到调用showDialog的页面,再处理。

    ///await showDialog方法的返回
    String result = await showDialog(
              context: context,
              builder: (context) {
                return AlertDialog(
                  content: new SingleChildScrollView(
                    child: ListBody(
                      children: <Widget>[Text("提示文字"), Text("")],
                    ),
                  ),
                  actions: <Widget>[
                    FlatButton(
                      child: Text("确定"),
                      onPressed: () async {
                        Map resultMap1;
                        try {
                          //HTTP请求
                        } catch (e) {
                          print('请求异常' + e.toString());
                        }
                        if(resultMap1["code"] == "0"){
                          Navigator.pop(context, "1"); //返回到上一个页面,退出showDialog页面
                        } else {
                          Scaffold.of(context).showSnackBar(SnackBar(
                              content: Text("替换失败"),
                              backgroundColor: Colors.red, 
                              duration: Duration(milliseconds: 500)
                          )
                          );
                        }
                      }
                    ),
                    FlatButton(
                      child: Text("取消"),
                      onPressed: () {
                        Navigator.pop(context);
                      },
                    )
                  ],
                );
              });
          return result; // 返回给_submit的回调方法,此时就可以接收到值了
        }
      }
    

    四、参考资料:

    Flutter更新showDialog中的内容 - 简书

    相关文章

      网友评论

        本文标题:flutter showDialog回调的问题

        本文链接:https://www.haomeiwen.com/subject/xpbvbctx.html