Skip to content

Instantly share code, notes, and snippets.

@erluxman
Last active June 19, 2020 07:19
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 erluxman/f8233e822afa073a90018c3bf8a9e271 to your computer and use it in GitHub Desktop.
Save erluxman/f8233e822afa073a90018c3bf8a9e271 to your computer and use it in GitHub Desktop.
Enum extension to get the name
void main() {
// ❌ Without Extension Function ❌
print(Countries.Cote_d_Ivoire.toString().split('.').last.replaceAll("_", " ")); // Cote d Ivoire
print(Movies.Romance.toString().split('.').last.replaceAll("_", " ")); //Romance
// ✅ With Extension Function ✅
print(Countries.Cote_d_Ivoire.enumValue); // Cote d Ivoire
print(Movies.Romance.enumValue); //Romance
}
enum Countries { United_States, United_Kingdom, Germany, Japan, Cote_d_Ivoire }
enum Movies { Romance, Science_Fiction, Romantic_Comedy, Martial_arts }
extension PrettyEnum on Object {
String get enumValue => this.toString().split('.').last.replaceAll("_", " ");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment