我目前正在使用 FLutter 和 firebase。我正在嘗試將一些資料上傳到云 Firestore。到現在為止還挺好。當我想將串列附加到欄位時遇到問題。如前所述,此欄位包含定義如下的值串列:
Map<String, Object> toDocument() {
Map customerAddress = Map();
Map orderCheckoutDetails = Map();
final DateTime now = DateTime.now();
final DateFormat formatter = DateFormat('dd-MM-yyyy');
final String formatted = formatter.format(now);
customerAddress['address'] = this.address;
customerAddress['city'] = this.city;
customerAddress['state'] = this.state;
customerAddress['zipCode'] = this.zipCode;
orderCheckoutDetails['checkoutOrderDate'] = formatted;
orderCheckoutDetails['customerAddress'] = customerAddress;
orderCheckoutDetails['customerName'] = '${this.nome!} ${this.cognome!}';
orderCheckoutDetails['customerPhone'] = this.numeroTelefono!;
orderCheckoutDetails['products'] = this.products!.map((product) => product.name).toList();
orderCheckoutDetails['subtotal'] = this.subtotal!;
orderCheckoutDetails['deliveryFee'] = this.deliveryFee!;
orderCheckoutDetails['total'] = this.total!;
List<Map<dynamic, dynamic>> orderList = [orderCheckoutDetails];
return {
'orderCheckoutDetails': orderList,
};
}
這就是它向我顯示 Firebase 上的專案的方式(這是正確的)。 在此處輸入圖片說明
這就是我將檔案上傳到 Firebase 的方式。
@override
Future<void> addCheckout(Checkout checkout) async {
print(checkout.email!);
final docExists = await _checkIfUserCheckoutExists(checkout.email!);
print(docExists);
if (!docExists) {
return _checkoutCollection
.doc(checkout.email!)
.set(checkout.toDocument());
} else {
await _checkoutCollection.doc(checkout.email!).update({
"orderCheckoutDetails":
FieldValue.arrayUnion(checkout.toDocument() as List)
});
}
}
我想要做的是在檔案的末尾附加另一個元素(通過引數傳遞的 checkout 元素)。我怎樣才能做到這一點?
uj5u.com熱心網友回復:
您可以set在兩種情況下使用合并選項(無論檔案是否存在),它將根據需要創建或更新檔案。另外,您的toDocument方法應該回傳orderList自身,而不是您當前回傳的地圖。
嘗試這個:
Map<dynamic, dynamic> toDocument() {
Map customerAddress = Map();
Map<dynamic, dynamic> orderCheckoutDetails = Map();
final DateTime now = DateTime.now();
final DateFormat formatter = DateFormat('dd-MM-yyyy');
final String formatted = formatter.format(now);
customerAddress['address'] = this.address;
customerAddress['city'] = this.city;
customerAddress['state'] = this.state;
customerAddress['zipCode'] = this.zipCode;
orderCheckoutDetails['checkoutOrderDate'] = formatted;
orderCheckoutDetails['customerAddress'] = customerAddress;
orderCheckoutDetails['customerName'] = '${this.nome!} ${this.cognome!}';
orderCheckoutDetails['customerPhone'] = this.numeroTelefono!;
orderCheckoutDetails['products'] = this.products!.map((product) => product.name).toList();
orderCheckoutDetails['subtotal'] = this.subtotal!;
orderCheckoutDetails['deliveryFee'] = this.deliveryFee!;
orderCheckoutDetails['total'] = this.total!;
return orderCheckoutDetails;
}
然后創建/插入您的檔案,如:
return _checkoutCollection
.doc(checkout.email!)
.set({
'orderCheckoutDetails' : FieldValue.arrayUnion([checkout.toDocument])
}, SetOptions(merge: true));
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/367090.html
標籤:火力基地 扑 镖 谷歌云firestore
