0

i'm trying to do the upload of image files to the cloud firestore in firebase using flutter. i'm always getting the following error and I do not understand where the issue is.

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

console:

E/StorageException(10183): StorageException has occurred.
E/StorageException(10183): Object does not exist at location.
E/StorageException(10183):  Code: -13010 HttpResult: 404

Here is my code:

class DatabaseMethods {

  CollectionReference cartesPro =
      FirebaseFirestore.instance.collection('cartesPro');
  FirebaseStorage storage = FirebaseStorage.instance;

  Future<String> uploadFile(file) async {
    Reference reference = storage.ref().child('cartesPro/');
    print('REFERENCE: ${reference}');
    UploadTask uploadTask = reference.putFile(file);
    print('UPLOAD TASK${uploadTask.snapshot}');
    TaskSnapshot taskSnapshot = await uploadTask;
    return await taskSnapshot.ref.getDownloadURL();
  }

  void addFile(CartPro cartPro) {
    cartesPro.add({
      "cartUserId": cartPro.cartUserId,
      "cartTimestamp": FieldValue.serverTimestamp()
    });
  }
}

Log of reference and uploadTask snapshot:

I/flutter (10183): REFERENCE: Reference(app: [DEFAULT], fullPath: cartesPro)
I/flutter (10183): UPLOAD TASK: TaskSnapshot(ref: Reference(app: [DEFAULT], fullPath: cartesPro), state: TaskState.running)

Everything seems pretty fine to me but still I do always get that error! I'd be grateful for any kind of help!

4
  • 404 mean you are not authorized to perform this action, you need to update read write rule in firebase Commented Dec 1, 2022 at 8:11
  • Rules are set up as follows: rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { match /{document=**} { allow read, write: if request.time < timestamp.date(2022, 12, 30); } } } I tried to remove the condition by leaving it only: allow read, write but still having the same error Commented Dec 1, 2022 at 8:52
  • print this taskSnapshot.ref.getDownloadURL(), and see what you got Commented Dec 1, 2022 at 10:02
  • It's not even getting to that line, the error appears at TaskSnapshot taskSnapshot = await uploadTask; Commented Dec 1, 2022 at 11:06

1 Answer 1

3

First, make sure your firebase storage rules are correct.

Example:

rules_version = '2';
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      allow read, write;
      
    }
  }
}

Second, try pushing the data that comes as File as Uint8List.

Example:

Future<String> uploadFile(File file, String uid) async {
    Reference reference = storage.ref().child('cartesPro/').child(uid); //It's helpful to name your file uniquely
    Uint8List bytes = file.readAsBytesSync(); //THIS LINE
    var snapshot = await reference.putData(bytes); //THIS LINE
    return await snapshot.ref.getDownloadURL();
  }
Sign up to request clarification or add additional context in comments.

2 Comments

Accepting your answer as it resolved the issue, I had to fix the security rules correctly! Thank you so much!
For me, had to change putFile to putData as you did in your second snippet. Thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.