首页 > 解决方案 > 无法从 Firebase 存储中获取下载网址

问题描述

我可以成功上传图片,但无法获取上传图片的 URL 另外随着最近方法的变化,我想写一个比这更干净的代码。

try {
          await FirebaseStorage.instance
              .ref()
              .child('user_image')
              .child(authResult.user
                      .uid + //ref gives access to root cloud store , firebase manages all the tokens etc
                  '.jpg')
              .putFile(image);
        } on FirebaseException catch (e) {
          print(e);
        }
        //Download the Url
        String url = await FirebaseStorage.instance
            .ref()
            .child('user_image')
            .child(authResult.user
                    .uid + //ref gives access to root cloud store , firebase manages all the tokens etc
                '.jpg')
            .getDownloadURL();
        print('Url' + url);


标签: flutterdartfirebase-storage

解决方案


我使用以下代码进行上传和下载。您错过了使用 uploadtask 属性。

   final photoRef = FirebaseStorage.instance.ref().child("photos"); 
   UploadTask uploadTask = photoRef.child("$id.jpg").putFile(photo);
   String url = await (await uploadTask).ref.getDownloadURL();

推荐阅读