1+ import 'dart:convert' ;
2+
13class Product {
24 final String brand;
35 final String category;
@@ -10,15 +12,113 @@ class Product {
1012 final double trendiness;
1113 final String productUrl;
1214
13- Product (
14- {required this .brand,
15- required this .category,
16- required this .desc,
17- required this .imageUrl,
18- required this .name,
19- required this .price,
20- required this .productUrl,
21- required this .rating,
22- required this .reviews,
23- required this .trendiness});
15+ Product ({
16+ required this .brand,
17+ required this .category,
18+ required this .desc,
19+ required this .imageUrl,
20+ required this .name,
21+ required this .price,
22+ required this .rating,
23+ required this .reviews,
24+ required this .trendiness,
25+ required this .productUrl,
26+ });
27+
28+ Product copyWith ({
29+ String ? brand,
30+ String ? category,
31+ String ? desc,
32+ String ? imageUrl,
33+ String ? name,
34+ int ? price,
35+ double ? rating,
36+ int ? reviews,
37+ double ? trendiness,
38+ String ? productUrl,
39+ }) {
40+ return Product (
41+ brand: brand ?? this .brand,
42+ category: category ?? this .category,
43+ desc: desc ?? this .desc,
44+ imageUrl: imageUrl ?? this .imageUrl,
45+ name: name ?? this .name,
46+ price: price ?? this .price,
47+ rating: rating ?? this .rating,
48+ reviews: reviews ?? this .reviews,
49+ trendiness: trendiness ?? this .trendiness,
50+ productUrl: productUrl ?? this .productUrl,
51+ );
52+ }
53+
54+ Map <String , dynamic > toMap () {
55+ return {
56+ 'brand' : brand,
57+ 'category' : category,
58+ 'desc' : desc,
59+ 'imageUrl' : imageUrl,
60+ 'name' : name,
61+ 'price' : price,
62+ 'rating' : rating,
63+ 'reviews' : reviews,
64+ 'trendiness' : trendiness,
65+ 'productUrl' : productUrl,
66+ };
67+ }
68+
69+ factory Product .fromMap (Map <String , dynamic > map) {
70+ return Product (
71+ brand: map['brand' ],
72+ category: map['category' ],
73+ desc: map['desc' ],
74+ imageUrl: map['imageUrl' ],
75+ name: map['name' ],
76+ price: map['price' ],
77+ rating: map['rating' ],
78+ reviews: map['reviews' ],
79+ trendiness: map['trendiness' ],
80+ productUrl: map['productUrl' ],
81+ );
82+ }
83+
84+ String toJson () => json.encode (toMap ());
85+
86+ factory Product .fromJson (String source) =>
87+ Product .fromMap (json.decode (source));
88+
89+ @override
90+ String toString () {
91+ return 'Product(brand: $brand , category: $category , desc: $desc , imageUrl: $imageUrl , name: $name , price: $price , rating: $rating , reviews: $reviews , trendiness: $trendiness , productUrl: $productUrl )' ;
92+ }
93+
94+ @override
95+ bool operator == (Object other) {
96+ if (identical (this , other)) return true ;
97+
98+ return other is Product &&
99+ other.brand == brand &&
100+ other.category == category &&
101+ other.desc == desc &&
102+ other.imageUrl == imageUrl &&
103+ other.name == name &&
104+ other.price == price &&
105+ other.rating == rating &&
106+ other.reviews == reviews &&
107+ other.trendiness == trendiness &&
108+ other.productUrl == productUrl;
109+ }
110+
111+ @override
112+ int get hashCode {
113+ return brand.hashCode ^
114+ category.hashCode ^
115+ desc.hashCode ^
116+ imageUrl.hashCode ^
117+ name.hashCode ^
118+ price.hashCode ^
119+ rating.hashCode ^
120+ reviews.hashCode ^
121+ trendiness.hashCode ^
122+ productUrl.hashCode;
123+ }
24124}
0 commit comments