繁体   English   中英

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

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

我想知道为什么在尝试上传图片时会出现此错误。

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;
  }
}

这是我的方法:

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

错误信息:

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)

文档中:

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

您需要获取存储桶的名称,但您将其传递给项目的url\path Go 到您的项目并获取您要使用的存储的存储桶名称。 这一切都假设您想使用除主 firebaseApp 之外的另一个存储桶。 否则,只需使用:

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

让生活更轻松。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM