当uploadTask完成时,getDownloadURL返回null,但是将图像上传到存储

时间:2019-08-18 16:58:11

标签: flutter google-cloud-firestore firebase-storage

我尝试上传到存储的图像是成功的,但是当我要使用上载的图像将URL存储到数据库中时,由于某种原因,它返回null。

Future<String> _uploadPic(String docRef, File file) {
FirebaseStorage _storage = FirebaseStorage.instance;
auth.currentUser().then((userID) async{
  StorageReference reference = _storage.ref().child(userID).child(docRef);
  StorageUploadTask uploadTask = reference.putFile(file);
  String downloadURL = (await uploadTask.onComplete).ref.getDownloadURL().toString();

  return downloadURL;
});

}

日志:

I/flutter (17507): debug: the uri is: null

2 个答案:

答案 0 :(得分:1)

Future<dynamic> _uploadPic(String docRef, File file) {

    FirebaseStorage _storage = FirebaseStorage.instance;

    // you need to return the result...
    return auth.currentUser().then((userID) async {
      StorageReference reference = _storage.ref().child(userID).child(docRef);
      StorageUploadTask uploadTask = reference.putFile(file);
      return (await uploadTask.onComplete).ref.getDownloadURL();
    });

}

答案 1 :(得分:0)

getDownloadURL()方法返回一个Future。因此,如果您对此调用toString(),则会得到future的字符串表示形式,而不是下载URL。相反,您需要等待getDownloadURL()的将来也完成,并且那个将为您提供URL。

String downloadURL = (await (await uploadTask.onComplete).ref.getDownloadURL()).toString();