Created
October 27, 2020 12:08
-
-
Save Vanethos/61ce1c1f665f7db5806ded167cd2e57a to your computer and use it in GitHub Desktop.
Enum extension
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
enum StarterPokemonEnum { bulbasaur, charmander, squirtle, } | |
/// This extension will give us the possibility to return a String | |
/// when we call the [name] getter | |
extension StarterPokemonToString on StarterPokemonEnum { | |
String get name { | |
switch (this) { | |
case StarterPokemonEnum.bulbasaur: | |
return "Bulbasaur"; | |
case StarterPokemonEnum.charmander: | |
return "Charmander"; | |
case StarterPokemonEnum.squirtle: | |
return "Squirtle"; | |
default: | |
return "Missigno"; | |
} | |
} | |
} | |
void main() { | |
/// Convert the list of all the values of the enum to a list of Strings | |
var startersList = | |
StarterPokemonEnum | |
/// Get all possible values for the enum | |
.values | |
/// Map them to their names | |
.map((pokemon) => pokemon.name) | |
.toList(); | |
/// Print the List | |
print(startersList); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment