Skip to content

Instantly share code, notes, and snippets.

@PlugFox
Last active June 15, 2023 07:26
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/ed18b5ce21c5286b5a95d2efa34958f1 to your computer and use it in GitHub Desktop.
Save PlugFox/ed18b5ce21c5286b5a95d2efa34958f1 to your computer and use it in GitHub Desktop.
Enum with Expando
/*
* Enum with expando properties
* https://gist.github.com/PlugFox/ed18b5ce21c5286b5a95d2efa34958f1
* https://dartpad.dev/ed18b5ce21c5286b5a95d2efa34958f1
* Matiunin Mikhail <plugfox@gmail.com>, 07 June 2022
*/
void main() {
const red = Color.red, green = Color.green, blue = Color.blue;
print('${red.color}, ${green.color}, ${blue.color}');
// <unknown>, <unknown>, <unknown>
red.color = 'red';
blue.color = 'blue';
print('${red.color}, ${green.color}, ${blue.color}');
// red, <unknown>, blue
print(Color.red.color);
// red
}
enum Color {
red,
green,
blue;
String get color => _expando[this] ?? '<unknown>';
set color(String color) => _expando[this] = color;
static final Expando<String> _expando = Expando<String>();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment