Created
August 21, 2022 15:11
-
-
Save matanlurey/b7fccf738276e6d129baa89f933cdd21 to your computer and use it in GitHub Desktop.
Provides an extension method to convert enum values to a map
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() { | |
// {Animals.cat: 0, Animals.dog: 1, Animals.pig: 2} | |
print(Animals.values.toMap()); | |
} | |
extension EnumHelper<E extends Enum> on Iterable<E> { | |
Map<E, int> toMap() { | |
return {for (final v in this) v: v.index}; | |
} | |
} | |
enum Animals { | |
cat, | |
dog, | |
pig, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment