【问题标题】:Async function doesn't await异步函数不等待
【发布时间】:2019-11-28 23:00:59
【问题描述】:

我正在尝试将图像上传到 firebase 存储,但是当我调用该函数时,未执行 await 以获取 url。我错过了什么?

查看其他主题,我发现问题可能出在“then”上,但是如何设置代码以等待 url?

Async/Await/then in Dart/Flutter

Future < String > uploadImage(File imageFile) async {
  String _imageUrl;
  StorageReference ref =
    FirebaseStorage.instance.ref().child(firebaseUser.uid.toString());

  await(ref.putFile(imageFile).onComplete.then((val) {
    val.ref.getDownloadURL().then((val) {
      _imageUrl = val;
      print(val);
      print("urlupload");
    });
  }));

  print(_imageUrl);
  print("urlnoupload");

  return _imageUrl;

}

谢谢!

【问题讨论】:

    标签: flutter


    【解决方案1】:

    当你不需要它时你有 async/await 因为你在 then 函数中捕获值

    Future<String> uploadImage(File imageFile) async {
      String _imageUrl;
      StorageReference ref = FirebaseStorage.instance.ref().child(firebaseUser.uid.toString());
      return ref.putFile(imageFile).onComplete.then((val) {
          return val.ref.getDownloadURL()
      }).then((_imageUrl) {
          return _imageUrl;
      });
    },
    

    【讨论】:

    • 它工作了亚伦,谢谢,我想我现在得到了“那么”。还有一件事,为什么你需要设置3次return?
    猜你喜欢
    • 2023-03-13
    • 2019-06-16
    • 2018-02-03
    • 1970-01-01
    • 1970-01-01
    • 2022-07-06
    • 2020-03-24
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多