Skip to content

Instantly share code, notes, and snippets.

@vovahost
Last active October 14, 2021 06:13
Show Gist options
  • Save vovahost/c4001d907d6a420cafb2bc2c2507f72c to your computer and use it in GitHub Desktop.
Save vovahost/c4001d907d6a420cafb2bc2c2507f72c to your computer and use it in GitHub Desktop.
Example of Enum with fields and methods implemented using Extensions. This example uses a constant list instead of switch.
main(List<String> arguments) {
final cat = Cat.white;
print('cat name: ${cat.name}');
cat.talk();
}
enum Cat {
black,
white,
grey,
}
extension CatExtension on Cat {
static const names = {
Cat.black: 'Black Cat',
Cat.white: 'White Cat',
Cat.grey: 'Grey Cat',
};
String? get name => names[this];
void talk() {
print('meow');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment