简体   繁体   English

如何使用flutter从firebase获取下载URL?

[英]How to get the download URL from firebase with flutter?

I would like to retrieve the Download URL of a private image in firbase storage.我想检索 firbase 存储中私有图像的下载 URL。

Hi have tried many of the suggestions on the site, but all of them end up in the same result.您好,已经尝试了网站上的许多建议,但最终都得到了相同的结果。

I have tried the following code:我尝试了以下代码:

getImageNow() async {
    StorageReference ref =
    FirebaseStorage.instance.ref().child("/1.jpg");
    String url = (await ref.getDownloadURL()).toString();
    return url;
  }

It works when i print the url inside the function, but when i try to call print(getImageNow()) to get the url, i just get "Instance of 'Future<dynamic>'"当我在函数内打印 url 时它起作用,但是当我尝试调用print(getImageNow())来获取 url 时,我只得到"Instance of 'Future<dynamic>'"

UPDATE*************更新*************

In the end i am trying to get somthing like this:最后,我试图得到这样的东西:

return Image.network(
  getImageNow(),
);

But i can not get it to work with async.但我无法让它与异步一起工作。

Since getImageNow() is asynchronous (as indicated by the async keyword), you will need to use await to make the calling code wait for the result:由于getImageNow()是异步的(如async关键字所示),您将需要使用await使调用代码等待结果:

print(await getImageNow())

What await does here is that it essentially unwraps the Future and is equivalent to: await在这里所做的是它本质上解开了Future并等效于:

getImageNow().then((value) => print(value));

this is how i upload and get the download url这就是我上传和获取下载网址的方式

this part is how i get image from picker这部分是我如何从选择器获取图像

 Future getImage() async {
  var image = await ImagePicker.pickImage(source: ImageSource.gallery);

  setState(() {
    _image = image;
      print('Image Path $_image');
  });
}

then i upload it然后我上传它

 Future uploadPic(BuildContext context) async {

  String fileName = basename(_image.path);
  StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(fileName);
  StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
  StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;


  final String url = (await taskSnapshot.ref.getDownloadURL());
  print('URL Is $url');
}

hope it will help someone希望它会帮助某人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM