runable
This commit is contained in:
@@ -13,7 +13,7 @@ class ProductModel extends HiveObject {
|
||||
final String name;
|
||||
|
||||
@HiveField(2)
|
||||
final String description;
|
||||
final String? description;
|
||||
|
||||
@HiveField(3)
|
||||
final double price;
|
||||
@@ -39,7 +39,7 @@ class ProductModel extends HiveObject {
|
||||
ProductModel({
|
||||
required this.id,
|
||||
required this.name,
|
||||
required this.description,
|
||||
this.description,
|
||||
required this.price,
|
||||
this.imageUrl,
|
||||
required this.categoryId,
|
||||
@@ -83,18 +83,25 @@ class ProductModel extends HiveObject {
|
||||
|
||||
/// Create from JSON
|
||||
factory ProductModel.fromJson(Map<String, dynamic> json) {
|
||||
// Handle price as string or number from API
|
||||
final priceValue = json['price'];
|
||||
final price = priceValue is String
|
||||
? double.parse(priceValue)
|
||||
: (priceValue as num).toDouble();
|
||||
|
||||
return ProductModel(
|
||||
id: json['id'] as String,
|
||||
name: json['name'] as String,
|
||||
description: json['description'] as String,
|
||||
price: (json['price'] as num).toDouble(),
|
||||
description: json['description'] as String?,
|
||||
price: price,
|
||||
imageUrl: json['imageUrl'] as String?,
|
||||
categoryId: json['categoryId'] as String,
|
||||
stockQuantity: json['stockQuantity'] as int,
|
||||
isAvailable: json['isAvailable'] as bool,
|
||||
stockQuantity: json['stockQuantity'] as int? ?? 0,
|
||||
isAvailable: json['isAvailable'] as bool? ?? true,
|
||||
createdAt: DateTime.parse(json['createdAt'] as String),
|
||||
updatedAt: DateTime.parse(json['updatedAt'] as String),
|
||||
);
|
||||
// Note: Nested 'category' object is ignored as we only need categoryId
|
||||
}
|
||||
|
||||
/// Convert to JSON
|
||||
|
||||
@@ -19,7 +19,7 @@ class ProductModelAdapter extends TypeAdapter<ProductModel> {
|
||||
return ProductModel(
|
||||
id: fields[0] as String,
|
||||
name: fields[1] as String,
|
||||
description: fields[2] as String,
|
||||
description: fields[2] as String?,
|
||||
price: (fields[3] as num).toDouble(),
|
||||
imageUrl: fields[4] as String?,
|
||||
categoryId: fields[5] as String,
|
||||
|
||||
Reference in New Issue
Block a user