I have three models I'm working with to retrieve and write data to my 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 model
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 model
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,
};
}
}
After watching the official Firestore videos on Maps vs Arrays vs Collection/documents, I've decided that sub collections / documents might just be the best for me due to how I am to display the information on my app.
- my toMap and fromMap methods, are they rightly implemented?
- Is this nested too deep?
- Should I leave Sales details and purchase items as maps or are they fine as their own classes?
- Is there a better way to my current implementation? What's the recommended standard of working with models who depends on other class models?
Thanks.