how to upload images to firebase storage then store the image url in firestore?

2,055

Try

String downloadUrl =
await (await uploadTask.onComplete).ref.getDownloadURL();

instead of your

Uri location = (await uploadTask.onComplete).uploadSessionUri;

As per docs of firebase UploadSessionUri is used to resume the upload of same file within a span of approx. week Here is the link for docs

Share:
2,055
Salma
Author by

Salma

Updated on December 09, 2022

Comments

  • Salma
    Salma over 1 year

    I'm trying to upload images to firebase storage and use that image url as metadata in firestore, however the image url is always null. (I'm assuming that the image hasn't finished uploading by the time i add it to my firestore).

    • this is how i get the picture:

            Future<File> getPic() async {      
            return picture = await ImagePicker.pickImage(source: 
            ImageSource.camera);
            }
      
    • how i upload the picture to firebase storage:

            Future<Uri> uploadPic(File image) async {    
            StorageReference reference = _storage.ref().child("images/");
            StorageUploadTask uploadTask = reference.putFile(image);
            Uri location = (await uploadTask.onComplete).uploadSessionUri;
            print('img location $location');
            return location;
          }
      
    • how i trigger the picture to be taken by pressing a FloatingActionButton:

           floatingActionButton: FloatingActionButton(
           onPressed: () {
           getPic().then((f) async => await uploadPic(f));
           }),
      

    Whenever i try to save this image uri to firestore it's always null. so what's the issue here?