如何在我的子集合中獲取串列產品并將其添加到我創建的資料模型中。我正在嘗試從供應商那里將所有產品添加到購物車.. 這是我的資料庫的結構

這是我想要做的資料代碼
class cartModel {
vendorCartModel? vendorCart;
List<productCartModel>? productsInCart;
cartModel();
cartModel.fromSnapshot(DocumentSnapshot snap){
vendorCart = vendorCartModel.fromSnapShot(snap);
productsInCart = ?????? (here is the part where i don't know what to do')
}
}
================
class vendorCartModel {
bool selected = false;
String? storeName;
String? uid;
String? userId;
String? vendorToken;
GeoPoint? storeLocation;
vendorCartModel();
vendorCartModel.fromSnapShot(DocumentSnapshot snap){
storeName = snap["storeName"];
uid = snap["uid"];
userId = snap["userId"];
vendorToken = snap["vendorToken"];
storeLocation = snap["storeLocation"];
}
}
==========================
class productCartModel {
bool selected = false;
String? name;
List<dynamic>? imgUrl;
String? uid;
String? vendorUid;
int? quantity;
double? sellingPrice;
double? sellerPrice;
double? oldPrice;
double? weight;
bool? shipping;
int? minOrder;
productCartModel();
productCartModel.fromSnapshot(DocumentSnapshot snap){
uid = snap["uid"];
vendorUid = snap["vendorUid"];
imgUrl = snap["imgUrl"];
name = snap["name"];
weight = snap["weight"];
sellingPrice = double.parse(snap["sellingPrice"].toString());
sellerPrice = double.parse(snap["sellerPrice"].toString());
oldPrice = double.parse(snap["oldPrice"].toString());
quantity = snap["quantity"];
minOrder = snap["minOrder"];
shipping = snap["shipping"];
}
}
================ 這是我的資料庫結構
- 購物車(集合)
- 欄位(這將用作供應商)
- 產品(子集)
- 欄位(這將用作供應商)
============== 這是我的查詢
await _fStoreIns
.collection('cart')
.doc(vendorId).get().then((value){
cartModel = cartModel.fromSnapshot(value);
})
提前致謝。
uj5u.com熱心網友回復:
用于更新資料
final pushData = FirebaseFirestore.instance.collection('cart');
pushData
.doc(cardId)
.collection("products")
.doc(productId)
.set({
changes:"Changes"
})
用于添加資料
final pushData = FirebaseFirestore.instance.collection('cart');
pushData
.doc(cardId)
.collection("products")
.add({
data: "DATA"
});
用于獲取資料
await pushData
.doc(productid)
.collection("products")
.orderBy("timeStamp", descending: true) // if you want to order by time or whatever
.limit(50) //for adding limit add limit
.get();
uj5u.com熱心網友回復:
如果我理解正確,您在 fbCloudStore 中有以下結構
-vendors (collection)
-vendor (doc)
-products (colecction)
-product (doc)
要獲取特定的子集合,您可以執行以下操作:
final vendorId = 'Replace with vendor Id';
final _querySnapshot = await _fStoreIns
.collection('vendors')
.doc(vendorId)
.collection('products')
.get();
for(final doc in _querySnapshot.docs){
//do stuff here
ProductModel.fromJson(doc.data());
}
uj5u.com熱心網友回復:
我的結構是這樣的
- cart (collection)
- fields (this will be used as vendor)
- products ( sub collection )
我想要的是這樣的
await _fStoreIns
.collection('cart')
.doc(vendorId).get().then((value){
cartModel = cartModel.fromSnapshot(value);
})
在我的 cartModel.fromSnapshot 里面
cartModel.fromSnapshot(DocumentSnapshot snap){
vendorCart = vendorCartModel.fromSnapShot(snap);
productCart.add(ChkOutItemModel.fromSnapshot(snap));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/337354.html
標籤:扑 谷歌云firestore 数据模型
上一篇:顫振:如何洗掉mapbox徽標
