Created
October 31, 2020 14:14
-
-
Save esaesa/ea2b66c1bccb3b275cb06f5e8ddd7b4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "dart:convert"; | |
void main() { | |
Cart cart= Cart(); | |
cart.name ="Arabs"; | |
Product p1 = Product(id: "11", name:"test1" , price: 100 ); | |
Product p3 = Product(id: "12", name:"test1" , price: 100); | |
Product p2 = Product(id: "13", name:"test2", price: 200); | |
CartProduct cp1 = CartProduct(qnt:"2", product:p1); | |
CartProduct cp3 = CartProduct(qnt:"3", product:p3); | |
cart.products.add(cp1); | |
cart.products.add(cp3); | |
print(jsonDecode(jsonEncode(cart))); | |
} | |
class Cart { | |
List<CartProduct> products = []; | |
String name; | |
toJson(){ | |
Map<String,dynamic> ls ={}; | |
ls["name"] = name; | |
//print(jsonEncode(products)); | |
products.forEach((p){ | |
// ls[p.product.id] = p.toJson() ; | |
}); | |
return {"name":name, "products":products}; | |
} | |
} | |
class CartProduct{ | |
CartProduct({this.product, this.qnt}); | |
Product product; | |
String qnt; | |
Map<String, dynamic> toJson(){ | |
return{ | |
"qnt": qnt, | |
"product": product, | |
}; | |
} | |
} | |
class Product{ | |
final name; | |
final id; | |
final double price; | |
Product({this.id,this.name, this.price}); | |
Map<String, dynamic> toJson() { | |
return { | |
'id': id, | |
'name': name, | |
'price': price | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment