如何在数据模型中添加子 collection?扑
How can i add the sub collection in a data model? flutter
如何在我的子 collection 中获取列表产品并将其添加到我创建的数据模型中。
我正在尝试从供应商处将所有产品添加到购物车..
这是我的数据库的结构
这是我正在尝试执行的数据代码
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"];
}
}
================
这是我的数据库结构
- 购物车(collection)
- 字段(这将用作供应商)
- 产品(子 collection)
==============
这是我的查询
await _fStoreIns
.collection('cart')
.doc(vendorId).get().then((value){
cartModel = cartModel.fromSnapshot(value);
})
提前致谢。
For Updating data
final pushData = FirebaseFirestore.instance.collection('cart');
pushData
.doc(cardId)
.collection("products")
.doc(productId)
.set({
changes:"Changes"
})
For adding data
final pushData = FirebaseFirestore.instance.collection('cart');
pushData
.doc(cardId)
.collection("products")
.add({
data: "DATA"
});
For getting 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();
如果我理解正确的话,你在 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());
}
我的结构是这样的
- 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));
}
首先,我们获取供应商的数据,然后获取产品数据,并将两者传递给 cartModel.fromSnapShot
方法。
使用这个查询:
DocumentSnapshot<Map<String, dynamic>> vendorData =
await _fStoreIns.collection('cart').doc(vendorId).get();
QuerySnapshot<Map<String, dynamic>> productsData = await _fStoreIns
.collection('cart')
.doc(vendorId)
.collection('products')
.get();
cartModel = cartModel.fromSnapshot(vendorData, productsData);
定义你的 cartModel.fromSnapshot
如下:
cartModel.fromSnapshot(DocumentSnapshot vendorData, QuerySnapshot productsData) {
vendorCart = vendorCartModel.fromSnapShot(vendorData);
productsInCart = productsData.docs
.map((e) => productCartModel.fromSnapShot(e)).toList();
}
如何在我的子 collection 中获取列表产品并将其添加到我创建的数据模型中。
我正在尝试从供应商处将所有产品添加到购物车..
这是我的数据库的结构
这是我正在尝试执行的数据代码
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"];
}
}
================ 这是我的数据库结构
- 购物车(collection)
- 字段(这将用作供应商)
- 产品(子 collection)
- 字段(这将用作供应商)
============== 这是我的查询
await _fStoreIns
.collection('cart')
.doc(vendorId).get().then((value){
cartModel = cartModel.fromSnapshot(value);
})
提前致谢。
For Updating data
final pushData = FirebaseFirestore.instance.collection('cart');
pushData
.doc(cardId)
.collection("products")
.doc(productId)
.set({
changes:"Changes"
})
For adding data
final pushData = FirebaseFirestore.instance.collection('cart');
pushData
.doc(cardId)
.collection("products")
.add({
data: "DATA"
});
For getting 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();
如果我理解正确的话,你在 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());
}
我的结构是这样的
- 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));
}
首先,我们获取供应商的数据,然后获取产品数据,并将两者传递给 cartModel.fromSnapShot
方法。
使用这个查询:
DocumentSnapshot<Map<String, dynamic>> vendorData =
await _fStoreIns.collection('cart').doc(vendorId).get();
QuerySnapshot<Map<String, dynamic>> productsData = await _fStoreIns
.collection('cart')
.doc(vendorId)
.collection('products')
.get();
cartModel = cartModel.fromSnapshot(vendorData, productsData);
定义你的 cartModel.fromSnapshot
如下:
cartModel.fromSnapshot(DocumentSnapshot vendorData, QuerySnapshot productsData) {
vendorCart = vendorCartModel.fromSnapShot(vendorData);
productsInCart = productsData.docs
.map((e) => productCartModel.fromSnapShot(e)).toList();
}