按下按钮时如何显示与FutureBuilder的对话框?

时间:2018-09-01 08:54:29

标签: firebase dart flutter future builder

我使用FutureBuilder上传图像FirebaseStorage,当我按下上传按钮时,我想显示一个等待对话框。图像在FireStorage上成功上传,但未显示任何内容。我的代码有什么问题?我认为FutureBuilder返回Widget并按下按钮无效。也许是问题所在。还是我以错误的方式称呼FutureBuilder。您对我的错误代码有任何提示或建议吗?

这是我的代码;



    Widget buildBody(BuildContext context) {
        return new SingleChildScrollView(
          child: new Column(
            children: [
              new SizedBox(
                width: double.infinity,
                child: new RaisedButton(
                  child: new Text('Upload'),
                  onPressed:  () {
                    futureBuilder();
                  },
                ),
              ),
            ],
          ),
        );
      }

     Future mediaUpload() async {
        final fileName = DateTime.now().millisecondsSinceEpoch.toString() + '.jpg';
        final StorageReference storage = FirebaseStorage.instance.ref().child(fileName);
        final StorageUploadTask task = storage.putFile(_image);
        return task.future;
      }

      Widget futureBuilder() {
        return new FutureBuilder(
          future: mediaUpload(),
          builder: (BuildContext context, AsyncSnapshot snapshot) {
            switch (snapshot.connectionState) {
              case ConnectionState.none:
                print('ConnectionState.none');
                return new Text('Press button to start.');
              case ConnectionState.active:
                print('ConnectionState.active');
                return new Text('');
              case ConnectionState.waiting:
                waitingDialog(context);
                return waitingDialog(context);
              case ConnectionState.done:
                print('ConnectionState.done');
                if (snapshot.hasError) return new Text('Error: ${snapshot.error}');
                print(snapshot.data.downloadUrl);
                Navigator.of(context).pushReplacementNamed('home');
                return new Text('Result: ${snapshot.data.downloadUrl}');
            }
          },
        );
      }

      waitingDialog(BuildContext context) {
        return showDialog(
          context: context,
          barrierDismissible: false,
          builder: (BuildContext context) {
            return new Center(
              child: new SizedBox(
                width: 40.0,
                height: 40.0,
                child: const CircularProgressIndicator(
                  value: null,
                  strokeWidth: 2.0,
                ),
              ),
            );
          },
        );
      }

1 个答案:

答案 0 :(得分:0)

我不确定100%为什么FutureBuilder不起作用。但是我有一个猜测。在以上示例中,FutureBuilder未附加到widgetTree(屏幕)。未附加表示从屏幕上不显示从FutureBuilder返回的。在这种情况下,将根据未附加到屏幕的Text返回snapshot.connectionState

解决方法: (我已经测试过并且可以正常工作,请验证一下)

futureBuilder(BuildContext context) {
  mediaUpload().then((task) {          // fire the upload
    Navigator.of(context).maybePop();  // remove the dialog on success upload
  });                                  // we can use task(which returned from
                                       // mediaUpload()) to get the values like downloadUrl.
  waitingDialog(context);             // show the spinner dialog
}