skip to Main Content

Context: I was trying to implement a feature on an app I have been developing to upload and store a photo, it was not working and I could not tell why as I felt my code was fine and searching on the internet did not resolve the issue. So I created a new application with just two buttons to see if I could store the image on there, and low and behold it magically works. Can anyone explain why and how I can get it to work on my original application.

Code:

Future selectFile() async {
    final result = await FilePicker.platform.pickFiles(
      allowMultiple: false,
      type: FileType.custom,
      allowedExtensions: ["png", "jpg"],
    );

    final path = result?.files.single.path;
    final name = result?.files.single.name;

    print(path);
    print(name);

    setState(() {
      file = File(path!);
      fileName = name;
    });
  }

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

    final destination = "test/${fileName}";

    FirebaseStorage.instance.ref(destination).putFile(file!);
  }

I just don’t understand how the exact same code can work on one application but not another, any help will be greatly appreciated.

For reference, this is the error I get when I try to upload an image on my original application.

I/flutter (27972): [firebase_storage/object-not-found] No object exists at the desired reference.

2

Answers


  1. Chosen as BEST ANSWER

    It was indeed a problem with the setup/project files of my application I returned to a commit before I had even touched Firebase and implemented everything from scratch and it now works on my original application. I am guessing when I first started messing around with firebase I installed the wrong version or something like that.


  2. So here you are using the upload file method as non static one and in the previous one you used a class directly without an instance of it..

    For example

    class Storage{
      uploadFile(){}
    }
    

    You should be using an instance of storage like

    Storage _storage = Storage();
    //Now to access any method in this class use it's instance as reference
    _storage.uploadFile();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search