2

I would like to know why i get this error when I try to upload an image.

class StorageRepository {
  FirebaseStorage storage = FirebaseStorage.instanceFor(
    bucket:
        '', (Edit: The link was only wrong)
  );

  Future<String> uploadFile(File file) async {
    var userUID = getCurrentUsersUID();
    var storageRef = storage.ref().child("user/profile/$userUID");
    UploadTask uploadTask = storageRef.putFile(file);
    String downloadUrl = await (await uploadTask).ref.getDownloadURL();
    userInstance.avatarUrl = downloadUrl;

    return downloadUrl;
  }
}

This is my method:

onPressed: () async {
   var image = await ImagePicker().getImage(source: ImageSource.gallery);
   var imageFile = File(image.path);
   StorageRepository().uploadFile(imageFile);
   setState(() {
    });
   },

Error Message:

E/flutter (20967): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: PlatformException(firebase_storage, The storage Uri cannot contain a path element., {}, null)

1 Answer 1

3

From the documentation:

 FirebaseStorage storage =  FirebaseStorage.instanceFor(
      bucket: 'secondary-storage-bucket');

You need to get your bucket's name, but you are passing it the url\path of a project. Go to your project and get the bucket name of the storage you want to use. This is all assuming that you want to use another storage bucket besides your main firebaseApp. Otherwise, just use:

Future<String> fireBaseUploadFile(String filePath) async {
FirebaseStorage storage = FirebaseStorage.instance;
   Reference ref = FirebaseStorage.instance.ref('/productImages/').child('someimageID');
  File file = File(filePath);

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

Makes life easier.

Sign up to request clarification or add additional context in comments.

Comments

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.