Oliver Olsen
Oliver Olsen

Reputation: 159

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.

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>'"

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.

Upvotes: 0

Views: 413

Answers (2)

eko
eko

Reputation: 672

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

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

Reputation: 599956

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:

print(await getImageNow())

What await does here is that it essentially unwraps the Future and is equivalent to:

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

Upvotes: 2

Related Questions