Skip to content

Instantly share code, notes, and snippets.

@bubnenkoff
Created July 7, 2020 09:07
Show Gist options
  • Select an option

  • Save bubnenkoff/3126dc3ffac5997f32cb46d2fe18a624 to your computer and use it in GitHub Desktop.

Select an option

Save bubnenkoff/3126dc3ffac5997f32cb46d2fe18a624 to your computer and use it in GitHub Desktop.
main() {
Map json = jsonDecode('{"id": 1, "price": 100, "type": "fruits", "color": "red"}');
ProductFactory productFactory = ProductFactory.fromJson(json);
}
class ProductFactory {
CurrentProduct currentProduct;
ProductFactory.fromJson(Map json)
{
if (json['type'] == 'bottles')
{
currentProduct = Bottles(json);
}
if (json['type'] == 'fruits')
{
currentProduct = Apples(json);
}
}
}
abstract class CurrentProduct
{
int id;
num price;
CurrentProduct(Map json)
{
this.id = json['id'];
this.price = json['price'];
}
}
class Bottles extends CurrentProduct {
String content;
Bottles(Map json): super(json)
{
this.content = json['content'];
}
}
class Apples extends CurrentProduct {
String color;
Apples(Map json): super(json)
{
this.color = json['color'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment