[英]Flutter Firestore Storage get downloadUrl
我需要通過將照片上傳到 Firebase 存儲來獲取下載 URL,以便將其存儲在 Firestore 文檔中。 我的代碼的問題是保存的 URL 不是 https//:所以我需要獲取下載 URL。 我想知道我需要在哪里調用它來獲取 downloadUrl 並將其保存在我的 Firestore 數據庫中。
這是我的代碼:
Future<void> _uploadProfilePhoto(String inputSource) async {
final picker = ImagePicker();
PickedFile? pickedImage;
try {
pickedImage = await picker.getImage(
source: inputSource == 'camera'
? ImageSource.camera
: ImageSource.gallery,
maxWidth: 1920);
final String fileName = path.basename(pickedImage!.path);
File imageFile = File(pickedImage.path);
try {
await storage.ref("avatars/$fileName").putFile(
imageFile,
SettableMetadata(customMetadata: {
'uploaded_by': '$uid',
}));
// Create/Update firesotre document
users.doc(uid).update({
"profilePhoto": fileName,
});
setState(() {});
} on FirebaseException catch (error) {
print(error);
}
} catch (err) {
print(err);
}
}
文件上傳完成后,您可以隨時在引用上調用getDownloadURL()
。 所以這將是一個好地方:
await storage.ref("avatars/$fileName").putFile(
imageFile,
SettableMetadata(customMetadata: {
'uploaded_by': '$uid',
}));
var downloadURL = await storage.ref("avatars/$fileName")..getDownloadURL();
// Create/Update firesotre document
users.doc(uid).update({
"profilePhoto": downloadURL,
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.