Flutter / Dart Convert Int to Enum
int idx = 2;
print(ThemeColor.values[idx]);
should give you
ThemeColor.blue
In Dart 2.17, you can use enhanced enums with values (which could have a different value to your index). Make sure you use the correct one for your needs. You can also define your own getter on your enum.
//returns Foo.one
print(Foo.values.firstWhere((x) => x.value == 1));
//returns Foo.two
print(Foo.values[1]);
//returns Foo.one
print(Foo.getByValue(1));
enum Foo {
one(1),
two(2);
const Foo(this.value);
final num value;
static Foo getByValue(num i){
return Foo.values.firstWhere((x) => x.value == i);
}
}
You can use:
ThemeColor.red.index
should give you
0