Created
May 31, 2020 18:40
-
-
Save veetaw/8ae439a8d84d197c030363675c85cbf0 to your computer and use it in GitHub Desktop.
extension
This file contains 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
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