Skip to content

Instantly share code, notes, and snippets.

@bubnenkoff
Last active July 7, 2020 08:02
Show Gist options
  • Save bubnenkoff/cae10cf1d91ee08c7251990e095aaf02 to your computer and use it in GitHub Desktop.
Save bubnenkoff/cae10cf1d91ee08c7251990e095aaf02 to your computer and use it in GitHub Desktop.
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