Skip to content

Instantly share code, notes, and snippets.

@matanlurey
Created August 21, 2022 15:11
Show Gist options
  • Save matanlurey/b7fccf738276e6d129baa89f933cdd21 to your computer and use it in GitHub Desktop.
Save matanlurey/b7fccf738276e6d129baa89f933cdd21 to your computer and use it in GitHub Desktop.
Provides an extension method to convert enum values to a map
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