Data Models
Dart model classes, serialization, and structure
Data Models
BabyShopHub uses strongly-typed Dart model classes to represent Firestore data. All models include fromJson (or fromMap) and toJson methods to facilitate easy parsing.
Directory Structure
Models are located in lib/models/.
Key Models
UserModel
Represents the data stored in users/{uid}.
ProductModel
Represents an item in the products collection. Uses cloud_firestore Timestamp for dates.
CartItemModel
A local model combining a ProductModel snapshot and a quantity.
OrderModel
Represents a finalized transaction.
Best Practices
- Immutability: Most model fields should be
final. Create acopyWithmethod if mutation is necessary. - Null Safety: Always provide fallback values (e.g.,
map['name'] ?? 'Unknown') infromMapfactories to prevent crashes when Firestore documents have missing fields. - Date Parsing: Firestore returns
Timestampobjects. Convert them to DartDateTimein thefromMapfactory:(map['createdAt'] as Timestamp).toDate().