Mt Khalifa
Mt Khalifa

Reputation: 498

how to get image URL form firebase storage to firestore

i am trying to upload the image to firebase and get the URL in firestore. It does upload the image but it does not get the URL to Firestore.

this is how i get the image.

Future getImage1() async {
    // ignore: deprecated_member_use
    var firstImage = await ImagePicker.pickImage(
        source: ImageSource.gallery, imageQuality: 65);
    setState(() {
      _image1 = firstImage;
    });
  }

this is how i upload and get the url.

  uploadPic() async {
    Reference ref = storage.ref().child("image1" + DateTime.now().toString());
    UploadTask uploadTask = ref.putFile(_image1);
    uploadTask.then((res) {
      String url = res.ref.getDownloadURL().toString();
      imageUrl1 = url;
      return url;
    });
  }

this is how i get the link in firestore

"image 1 Url":(_image1 != null) ? await uploadPic() : null,

Upvotes: 0

Views: 886

Answers (1)

Farhan Syah
Farhan Syah

Reputation: 903

getDownloadURL is a Future, so how about you try it like this:

Future<String> uploadPic() async {
    Reference ref = storage.ref().child("image1" + DateTime.now().toString());
    UploadTask uploadTask = ref.putFile(_image1);
    String url;
    await uploadTask.whenComplete(() {
      res.ref.getDownloadURL().then((fileUrl){
      url = fileUrl as String;
      });
    });
    return url;
  }

Upvotes: 2

Related Questions