Last active
July 8, 2020 04:54
-
-
Save ialameh/96db8db1c8833627265717ce5c40bf1f to your computer and use it in GitHub Desktop.
This file contains hidden or 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 itema = Cart(product: 'icecrea', price: '100'); | |
Cart itemb = Cart(product: 'tofu', price: '200'); | |
List<Cart> items = [itema, itemb]; | |
print(items); | |
String itemstosavetodatabase = jsonEncode(items); | |
print(itemstosavetodatabase); | |
List<dynamic> fromdatabase = jsonDecode(itemstosavetodatabase); | |
for(var item in fromdatabase) { | |
print(Cart.fromJson(item)); | |
} | |
} | |
class Cart { | |
String product; | |
String price; | |
Cart({this.product, this.price}); | |
Cart.fromJson(Map<String, dynamic> json) { | |
product = json['product']; | |
price = json['price']; | |
} | |
Map<String, dynamic> toJson() { | |
final Map<String, dynamic> data = new Map<String, dynamic>(); | |
data['product'] = this.product; | |
data['price'] = this.price; | |
return data; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment