我需要通過將照片上傳到 Firebase 存盤來獲取 downloadURL,以便我可以將其存盤在 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);
}
}
uj5u.com熱心網友回復:
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,
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/370360.html
標籤:火力基地 扑 谷歌云firestore 火力存储
