Flutter: Upload Image Unhandled Exception: PlatformException(firebase_storage, The storage Uri canno
Posted
技术标签:
【中文标题】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)Flutter: Upload Image Unhandled Exception: PlatformException(firebase_storage, The storage Uri cannot contain a path element., , null) 【发布时间】:2021-07-05 14:58:50 【问题描述】:我想知道为什么我在尝试上传图片时收到此错误。
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)
【问题讨论】:
【参考方案1】:来自documentation:
FirebaseStorage storage = FirebaseStorage.instanceFor(
bucket: 'secondary-storage-bucket');
您需要获取存储桶的名称,但您将项目的url\path
传递给它。转到您的项目并获取您要使用的存储的存储桶名称。这一切都假设您想使用除主 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();
让生活更轻松。
【讨论】:
以上是关于Flutter: Upload Image Unhandled Exception: PlatformException(firebase_storage, The storage Uri canno的主要内容,如果未能解决你的问题,请参考以下文章