Last active
October 14, 2021 06:13
-
-
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.
This file contains hidden or 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
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