data4autos_mobile_app/lib/data/models/product_model.dart
2025-11-29 22:28:02 +05:30

28 lines
703 B
Dart

import 'package:autos/domain/entities/product.dart';
class ProductModel extends ProductEntity {
ProductModel({
required super.id,
required super.name,
required super.image,
required super.price,
});
factory ProductModel.fromJson(Map<String, dynamic> json) {
return ProductModel(
id: json['id'] ?? 0,
/// ✅ SAFE STRING CONVERSION
name: json['name']?.toString() ?? '',
/// ✅ API sends `logo`, but entity expects `image`
image: json['logo']?.toString() ?? '',
/// ✅ API DOES NOT SEND PRICE → DEFAULT TO 0
price: json['price'] != null
? double.tryParse(json['price'].toString()) ?? 0.0
: 0.0,
);
}
}