【问题标题】:how to upload images to firebase storage then store the image url in firestore?如何将图像上传到firebase存储然后将图像url存储在firestore中?
【发布时间】:2019-07-24 03:43:03
【问题描述】:

我正在尝试将图像上传到 Firebase 存储并将该图像 url 用作 firestore 中的元数据,但是图像 url 始终为空。 (我假设图像在我将其添加到我的 Firestore 时尚未完成上传)。

  • 这就是我得到图片的方式:

          Future<File> getPic() async {      
          return picture = await ImagePicker.pickImage(source: 
          ImageSource.camera);
          }
    
  • 我如何将图片上传到 Firebase 存储:

          Future<Uri> uploadPic(File image) async {    
          StorageReference reference = _storage.ref().child("images/");
          StorageUploadTask uploadTask = reference.putFile(image);
          Uri location = (await uploadTask.onComplete).uploadSessionUri;
          print('img location $location');
          return location;
        }
    
  • 我如何通过按下 FloatingActionButton 来触发要拍摄的图片:

         floatingActionButton: FloatingActionButton(
         onPressed: () {
         getPic().then((f) async => await uploadPic(f));
         }),
    

每当我尝试将此图像 uri 保存到 firestore 时,它​​始终为空。那么这里的问题是什么?

【问题讨论】:

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


    【解决方案1】:

    试试

    String downloadUrl =
    await (await uploadTask.onComplete).ref.getDownloadURL();
    

    代替你的

    Uri location = (await uploadTask.onComplete).uploadSessionUri;
    

    根据 firebase 的文档,UploadSessionUri 用于恢复上传 在大约的跨度内相同的文件。周Here is the link for docs

    【讨论】:

      猜你喜欢
      • 2021-03-19
      • 2021-10-14
      • 1970-01-01
      • 2021-03-14
      • 2020-07-27
      • 2017-09-25
      相关资源
      最近更新 更多