Home > OS >  Not able to upload file to firebase storage in Flutter
Not able to upload file to firebase storage in Flutter

Time:12-01

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!

CodePudding user response:

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();
  }
  • Related