![](/img/trans.png)
[英]How to display Image in App with flutter and firebase using URL as String in Firestore
[英]How to store image in flutter firestore and display it
試圖將圖像存儲在圖書收藏 flutter 但不讓我稍后存儲和檢索它。 請幫我追蹤錯誤。 問題在於coverImage 不允許我將圖像URL 存儲在firebase 中,以便以后獲取圖像時可以顯示該圖像。
if (name != null &&
price != null &&
author != null &&
description != null &&
discountpercentage != null &&
rating != null) {
if (action == 'create') {
await books_collection.add({
"name": name,
"price": price,
"author": author,
"description": description,
"discountpercentage": discountpercentage,
"rating": rating,
"createdAt": Timestamp.now(),
"coverImage": Timestamp.now(),
// "url": uploadFilterImage()
});
}
if (action == 'update') {
// Update the product
await books_collection.doc(documentSnapshot!.id).update({
"name": name,
"price": price,
"author": author,
"description": description,
"discountpercentage": discountpercentage,
"rating": rating,
"createdAt": Timestamp.now(),
"coverImage": uploadFilterImage()
});
Future<String> uploadFilterImage() async {
String url = "";
final ImagePicker _picker = ImagePicker();
XFile? image = await _picker.pickImage(source: ImageSource.gallery);
Uint8List fileBytes = await image!.readAsBytes();
if (fileBytes != null) {
final _firebaseStorage = FirebaseStorage.instance;
var name = Timestamp.now().millisecondsSinceEpoch;
print("$name");
var snapshot = _firebaseStorage.ref().child('$name');
print("$name");
TaskSnapshot task = await snapshot.putData(
fileBytes,
SettableMetadata(contentType: 'image/jpeg'),
);
url = await task.ref.getDownloadURL();
if (url.isNotEmpty) {
Get.snackbar("successfull", "image uploaded");
}
}
return url;
}
您需要await
uploadFilterImage()
調用以處理異步 function 並且不返回 Future,而是返回最終值。 您的create
和update
案例也是如此。
await books_collection.doc(documentSnapshot!.id).update({
"name": name,
"price": price,
"author": author,
"description": description,
"discountpercentage": discountpercentage,
"rating": rating,
"createdAt": Timestamp.now(),
"coverImage": await uploadFilterImage() // here
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.