zweifärbbar
zweifärbbar

Reputation: 407

Flutter: Upload Image Unhandled Exception: PlatformException(firebase_storage, The storage Uri cannot contain a path element., {}, null)

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)

Upvotes: 2

Views: 415

Answers (1)

Huthaifa Muayyad
Huthaifa Muayyad

Reputation: 12383

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.

Upvotes: 3

Related Questions