我正在使用三种模型来检索数据并将数据写入Cloud Firestore。
//Sales model
class Sales {
String id;
String userId;
SalesDetails salesDetails;
Timestamp createdAt;
Timestamp updatedAt;
Sales();
Sales.fromMap(Map<String, dynamic> data) {
id = data['id'];
userId = data['user_id'];
salesDetails = SalesDetails.fromMap(data['sales_details']);
createdAt = data['created_at'];
updatedAt = data['updated_at'];
}
Map<String, dynamic> toMap() {
return {
'id': id,
'user_id': userId,
'sales_details': salesDetails,
'created_at': createdAt,
'updated_at': updatedAt,
};
}
}
// SalesDetails模型
class SalesDetails {
String id;
PurchasedItems purchaseItems;
String bankTransferSenderName;
String chequeInfo;
String creditPayLaterInfo;
String customerId;
String customerFirstName;
String customerLastName;
double subTotalAmount;
double totalAmount;
double taxesDue;
bool isComplete;
Timestamp createdAt;
Timestamp updatedAt;
SalesDetails();
SalesDetails.fromMap(Map<String, dynamic> data) {
id = data['id'];
purchaseItems = PurchasedItems.fromMap(data['purchased_items']);
bankTransferSenderName = data['bank_transfer_sender_name'];
chequeInfo = data['cheque_info'];
creditPayLaterInfo = data['credit_pay_later_info'];
customerId = data['customer_id'];
customerFirstName = data['customer_first_name'];
customerLastName = data['customer_last_name'];
subTotalAmount = data['sub_total_amount'];
totalAmount = data['total_amount'];
taxesDue = data['taxes_due'];
isComplete = data['is_complete'];
createdAt = data['created_at'];
updatedAt = data['updated_at'];
}
Map<String, dynamic> toMap() {
return {
'id': id,
'purchased_items': purchaseItems,
'bank_transfer_sender_name': bankTransferSenderName,
'cheque_info': chequeInfo,
'credit_pay_later_info': creditPayLaterInfo,
'customer_id': customerId,
'customer_first_name': customerFirstName,
'customer_last_name': customerLastName,
'sub_total_amount': subTotalAmount,
'total_amount': totalAmount,
'taxes_due': taxesDue,
'is_complete': isComplete,
'created_at': createdAt,
'updated_at': updatedAt,
};
}
}
// PurchasedItem模型
class PurchasedItems {
String itemId;
String itemName;
String itemDescription;
int itemQuantity;
PurchasedItems();
PurchasedItems.fromMap(Map<String, dynamic> data) {
itemId = data['item_id'];
itemName = data['item_name'];
itemDescription = data['item_description'];
itemQuantity = data['itemQuantity'];
}
Map<String, dynamic> toMap() {
return {
'id': itemId,
'user_id': itemName,
'item_quantity': itemQuantity,
'item_description': itemDescription,
};
}
}
在观看了Maps,Arrays,Collection /文档上的Firestore官方视频后,我决定由于我在应用程序中显示信息的方式,sub collections / documents可能最适合我。
谢谢。
答案 0 :(得分:1)
我的toMap和fromMap方法,它们正确实现了吗?
他们对我来说很好。
这个嵌套太深了吗?
这是一个很小的比例,所以我不认为您嵌套太多,或者当您打算添加更多的yes时,最好考虑一种更好的嵌套方法。< / p>
我应该将销售详细信息和购买的商品留在地图中还是将其作为自己的课程?
如果要在其他部分重用它们,您是否真的打算在此方面做更多的事情,可以尝试将它们作为自己的类,但是如果不需要,则它们会很好。
当前实施是否有更好的方法?使用依赖于其他类模型的模型的推荐标准是什么?
看起来不错,实际上取决于您的需求。既然您说过要向Cloud Firestore写入和读取数据,我已经检查了文档(https://firebase.google.com/docs/firestore/query-data/get-data#java)并以Java Android示例为例,这应该是“推荐”的方法。