[英]How can i upload list of uploaded images into Firestore on flutter?
I uploaded multiple images into FirebaseStorage then saved URL image into Firestore, but only one URL image save into firestore.我将多张图像上传到 FirebaseStorage,然后将 URL 图像保存到 Firestore,但只有一张 URL 图像保存到 Firestore。
for (var img in _image) {
Reference firebaseStorage = FirebaseStorage.instance
.ref()
.child("images/${foldername}/${imagename}");
UploadTask uploadTask = firebaseStorage.putFile(img);
TaskSnapshot task = await uploadTask;
task.ref.getDownloadURL().then((value) {
FirebaseFirestore.instance.collection("items").doc(user.uid).set({
"item_name": item_name.text,
"item_description": item_description.text,
"item_price": item_price.text,
"typeofitem": currentvalueSelected,
"sizeofitem": FieldValue.arrayUnion([selectedsize]),
"images": FieldValue.arrayUnion([value])
});
});
}
Right now when the upload of an image is completed and you've gotten its download URL, you call FirebaseFirestore.instance.collection("items").doc(user.uid).set({...})
each time.现在,当图像的上传完成并且您已下载 URL 时,您每次调用FirebaseFirestore.instance.collection("items").doc(user.uid).set({...})
。 Calling set()
like this replaces the existing contents of the document, so you end up with the common fields and then only the download URL of the last image to be done.像这样调用set()
会替换文档的现有内容,因此您最终会得到公共字段,然后只下载要完成的最后一个图像的 URL。
You'll want to separate two types of writes:您需要区分两种类型的写入:
var docRef = FirebaseFirestore.instance.collection("items").doc(user.uid);
docRef.set({
"item_name": item_name.text,
"item_description": item_description.text,
"item_price": item_price.text,
"typeofitem": currentvalueSelected,
"sizeofitem": FieldValue.arrayUnion([selectedsize]),
});
for (var img in _image) {
Reference firebaseStorage = FirebaseStorage.instance
.ref()
.child("images/${foldername}/${imagename}");
UploadTask uploadTask = firebaseStorage.putFile(img);
TaskSnapshot task = await uploadTask;
task.ref.getDownloadURL().then((value) {
docRef.update({
"images": FieldValue.arrayUnion([value])
});
});
}
It is because you have called to set the document inside the for loop so the previous data gets overwritten after every iteration这是因为您已调用在 for 循环中设置文档,因此每次迭代后先前的数据都会被覆盖
You could use Future.wait
if your uploading a lot of images.如果您上传大量图片,您可以使用Future.wait
。
...
List<UploadTask> uploadImgToStorageFutures = [];
List<Future<String>> getImgUrlFutures = [];
for (var img in _image) {
Reference firebaseStorage = FirebaseStorage.instance
.ref()
.child("images/${foldername}/${imagename}");
UploadTask uploadTask = firebaseStorage.putFile(img);
uploadImgToStorageFutures.add(uploadTask);
}
await Future.wait(uploadImgToStorageFutures).then((tasks) => tasks.forEach((task) {
getImgUrlFutures.add(task.ref.getDownloadURL());
}));
await Future.wait(getImgUrlFutures).then((urls) =>
FirebaseFirestore.instance.collection("items").doc(user.uid).set({
"item_name": item_name.text,
"item_description": item_description.text,
"item_price": item_price.text,
"typeofitem": currentvalueSelected,
"sizeofitem": FieldValue.arrayUnion([selectedsize]),
"images": urls
}));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.