0
votes

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,
1
What are you expecting to see in Firestore? There's no code here that writes any documents there. Firebase Storage is a completely different product than Firestore.Doug Stevenson
You will have to make two network calls. One to upload the image to the storage, and then to store the image URL to firestore collection. you already have the uploading out of the way. you have to now add to firestore collection or document.urchmaney
I updated the question take a look. @DougStevensonMt Khalifa
I need your HELP @PeterHaddadMt Khalifa

1 Answers

1
votes

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;
  }