Home > Net >  Keep getting error when trying to upload image to Firebase Storage
Keep getting error when trying to upload image to Firebase Storage

Time:07-25

Here is my code:

 Future selectFile() async {
    final result =
        await ImagePicker.platform.pickImage(source: ImageSource.gallery);

    if (result == null) return;
    final path = result.path;

    setState(() {
      file = File(path);
    });
  }

  Future uploadFile() async {
    if (file == null) return;

    final fileName = file!.path.split("/").last;
    final destination = "test/";

    Storage.uploadFile(destination, file!);
  }
import 'dart:io';

import 'package:firebase_storage/firebase_storage.dart';

class Storage {
  static Future<void> uploadFile(String destination, File file) async {
    try {
      final ref = FirebaseStorage.instance.ref().child(destination);

      await ref.putFile(file);
    } on FirebaseException catch (e) {
      print(e);
    }
  }
}

I can't seem to work out why exactly the code does not upload a photo, I have changed the rules in firebase but to no avail and the folder is called test so if anyone could suggest what I do or how to test my firebase storage, that would be a great help. Thanks in advance.

I keep getting this error when I call uploadFile:

E/StorageException(27972):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:272)
E/StorageException(27972):  at com.google.firebase.storage.network.NetworkRequest.performRequest(NetworkRequest.java:289)
E/StorageException(27972):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:76)
E/StorageException(27972):  at com.google.firebase.storage.internal.ExponentialBackoffSender.sendWithExponentialBackoff(ExponentialBackoffSender.java:68)
E/StorageException(27972):  at com.google.firebase.storage.UploadTask.sendWithRetry(UploadTask.java:477)
E/StorageException(27972):  at com.google.firebase.storage.UploadTask.beginResumableUpload(UploadTask.java:276)
E/StorageException(27972):  at com.google.firebase.storage.UploadTask.run(UploadTask.java:224)
E/StorageException(27972):  ... 5 more
I/flutter (27972): [firebase_storage/object-not-found] No object exists at the desired reference.

Firebase rules are:

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

UPDATE

I created a new flutter application with the above code and just two buttons, so it seems it may be a flaw in my other application's dependencies or something like that rather than the code. Thank you to everyone who helped once I figure out how to get it to work on my original application I will update.

Firebase Storage working on one application but not another

CodePudding user response:

The reference you created should look like this now you added ref to the destination. please try the following

final ref = FirebaseStorage.instance.ref().child('$destination/${file.name}.${file.extension}');

EDIT

final ref = FirebaseStorage.instance.ref();


final uploadTask = ref.child('$destination/${file.name}.${file.extension}')
    .putFile(file, SettableMetadata(
    contentType: "image/jpeg",
  ));

Edit1*

Remove static from here.

import 'dart:io';

import 'package:firebase_storage/firebase_storage.dart';

class Storage {
  Future<void> uploadFile(String destination, File file) async {
    try {
      final ref = FirebaseStorage.instance.ref().child(destination);

      await ref.putFile(file);
    } on FirebaseException catch (e) {
      print(e);
    }
  }
}

When you wish to upload first make a reference

Storage _storage = Storage();
_storage.uploadFile(destination, file!);
  

This should work as expected

CodePudding user response:

I have the similar problem and it solve like this.

await ref.putData(await file.readAsBytes());
  • Related