firebase_storage object-not-found No object exists at the desired reference flutter

Important: I found the same question but it is closed with incomplete debugging information.

I am uploading an image to firebase storage and then getting the download URL of that image and storing it to firebase so I can use that URL to show the User's profile image using network image.

It was working fine before when I was storing the image like

Reference storageRef = FirebaseStorage.instance.ref('images');
      file = await _compressImage(file: file,);
      await storageRef.putFile(file);
      final String downloadUrl = await storageRef.child(id).getDownloadURL();
      return downloadUrl;

but after I am storing images in specific folders

Reference storageRef = FirebaseStorage.instance.ref('images');
      file = await _compressImage(file: file, id: id);
      await storageRef
          .child(Get.find<UserController>().user.username)
          .child(id)
          .putFile(file);
      final String downloadUrl = await storageRef.child(id).getDownloadURL();
      return downloadUrl;

it is showing this error.

 [firebase_storage/object-not-found] No object exists at the desired reference.

Here is the explainable code: I am storing downloadable URL in newImage variable

 String newImage;
      if (_controller.file != null) {
        newImage = await Database().uploadFile(
            file: _controller.file,
            id: Get.find<UserController>().user.username);
        print("new image: " + newImage.toString());
      }

But here when I am printing newImage's value it is printing null to console.

new image: null

Here is the second method to upload image to firebase storage.

Future<String> uploadFile({@required File file, @required String id}) async {
    try {
      file = await _compressImage(file: file, id: id);
      await storageRef
          .child(Get.find<UserController>().user.username)
          .child(id)
          .putFile(file);
      final String downloadUrl = await storageRef.child(id).getDownloadURL();
      return downloadUrl;
    } catch (e) {
      print(e);
    }
  }

Debug Console:

E/StorageException(11376): StorageException has occurred.
E/StorageException(11376): Object does not exist at location.
E/StorageException(11376):  Code: -13010 HttpResult: 404
E/StorageException(11376): {  "error": {    "code": 404,    "message": "Not Found.  Could not get object",    "status": "GET_OBJECT"  }}
E/StorageException(11376): java.io.IOException: {  "error": {    "code": 404,    "message": "Not Found.  Could not get object",    "status": "GET_OBJECT"  }}
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.parseResponse(NetworkRequest.java:434)
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.parseErrorResponse(NetworkRequest.java:451)
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.processResponseStream(NetworkRequest.java:442)
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
E/StorageException(11376):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:286)
E/StorageException(11376):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:70)
E/StorageException(11376):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:62)
E/StorageException(11376):  at com.google.firebase.storage.GetDownloadUrlTask.run(GetDownloadUrlTask.java:76)
E/StorageException(11376):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1162)
E/StorageException(11376):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:636)
E/StorageException(11376):  at java.lang.Thread.run(Thread.java:764)
I/flutter (11376): [firebase_storage/object-not-found] No object exists at the desired reference.
I/flutter (11376): new image: null

But when I check the firebase storage and the image is uploaded successfully there.

What I know is that image is successfully uploading to firebase storage but the above method somehow returning a downable URL before uploading the image.


Solution 1:

I lost many hours with the same problem as you and IDK why but it is working by creating the reference before using it:

    final FirebaseStorage feedStorage =
              FirebaseStorage.instanceFor(bucket: F.feedBucket);
    
          Reference refFeedBucket = feedStorage
              .ref()
              .child('venues')
              .child(auth.user.uid)
              .child('vibes')
              .child(p.basename(file.path));

  String downloadUrl;

  TaskSnapshot uploadedFile = await refFeedBucket.putFile(file);

  if (uploadedFile.state == TaskState.success) {
    downloadUrl = await refFeedBucket.getDownloadURL();
  }

  return downloadUrl;

Solution 2:

What worked for me was a bit unexpected. First, I had the code like this which is how many examples have it.

  Reference reference = storage.ref(filePath);

  UploadTask uploadTask = reference.putFile(imageToUpload);

  final storageSnapshot = uploadTask.snapshot;

  final downloadUrl = await storageSnapshot.ref.getDownloadURL();

Then, I decided to play around with it. I notice if I await the putFile call, it changes the return type, even though its not a future.

  Reference reference = storage.ref(filePath);

  final TaskSnapshot snapshot = await reference.putFile(imageToUpload);

  final downloadUrl = await snapshot.ref.getDownloadURL();

And what do you know, this worked! Very odd, considering it's not obvious you can await the putFile call.