Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Last active November 22, 2021 12:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PlugFox/66b37ded0c086c4045143fd2732c099a to your computer and use it in GitHub Desktop.
Save PlugFox/66b37ded0c086c4045143fd2732c099a to your computer and use it in GitHub Desktop.
Enum with extension
/*
* 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