Last active
July 7, 2020 08:02
-
-
Save bubnenkoff/cae10cf1d91ee08c7251990e095aaf02 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
main() { | |
Map json = jsonDecode('{"id": 1, "price": 100, "type": "fruits", "color": "red"}'); | |
Product product = Product.fromJson(json); | |
} | |
class Product { | |
int id; | |
num price; | |
CurrentProduct currentProduct; | |
Product.fromJson(Map json) | |
{ | |
this.id = json['id']; | |
this.price = json['price']; | |
if (json['type'] == 'bottles') | |
{ | |
currentProduct = Bottles(json['type']); | |
} | |
if (json['type'] == 'fruits') | |
{ | |
currentProduct = Apples(json['red']); | |
} | |
} | |
} | |
abstract class CurrentProduct | |
{ | |
} | |
class Bottles implements CurrentProduct { | |
String content; | |
Bottles(this.content); | |
} | |
class Apples implements CurrentProduct { | |
String color; | |
Apples(this.color); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment