Skip to content

Instantly share code, notes, and snippets.

@veetaw
Created May 31, 2020 18:40
Show Gist options
  • Save veetaw/8ae439a8d84d197c030363675c85cbf0 to your computer and use it in GitHub Desktop.
Save veetaw/8ae439a8d84d197c030363675c85cbf0 to your computer and use it in GitHub Desktop.
extension
enum ItemType { drink, pizza }
class Item {
String id;
String name;
double price;
ItemType type;
List<int> image;
Item({
this.id,
this.name,
this.price,
this.type,
this.image,
});
Item.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
price = json['price'];
type = ItemType.fromString(json['type']);
image = json['image'];
}
}
extension on ItemType {
ItemType fromString(String str) {
if (str.toLowerCase() == "drink") return ItemType.drink;
if (str.toLowerCase() == "pizza") return ItemType.pizza;
throw Exception();
}
String get asString => this == ItemType.drink ? "drink" : "pizza";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment