Last active
November 22, 2021 12:09
-
-
Save PlugFox/66b37ded0c086c4045143fd2732c099a to your computer and use it in GitHub Desktop.
Enum with 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 with extension | |
* https://gist.github.com/PlugFox/66b37ded0c086c4045143fd2732c099a | |
* https://dartpad.dev/66b37ded0c086c4045143fd2732c099a?id=&null_safety=true | |
*/ | |
enum Country { | |
ru, | |
gb, | |
kz, | |
fr, | |
} | |
extension CountryX on Country { | |
int get code => when<int>( | |
ru: 7, | |
gb: 44, | |
kz: 7, | |
fr: 33, | |
); | |
String get representation => when<String>( | |
ru: 'Россия', | |
gb: 'Англия', | |
kz: 'Казахстан', | |
fr: 'Франция', | |
); | |
T when<T extends Object?>({ | |
required T ru, | |
required T gb, | |
required T kz, | |
required T fr, | |
}) { | |
switch (this) { | |
case Country.ru: | |
return ru; | |
case Country.gb: | |
return gb; | |
case Country.kz: | |
return kz; | |
case Country.fr: | |
return fr; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment