Last active
June 19, 2020 07:19
-
-
Save erluxman/f8233e822afa073a90018c3bf8a9e271 to your computer and use it in GitHub Desktop.
Enum extension to get the name
This file contains 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
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