Flutter / Dart Convert Int to Enum
28,487
Solution 1
int idx = 2;
print(ThemeColor.values[idx]);
should give you
ThemeColor.blue
Solution 2
You can use:
ThemeColor.red.index
should give you
0
Author by
henrykodev
Updated on June 05, 2021Comments
-
henrykodev almost 3 years
Is there a simple way to convert an integer value to enum? I want to retrieve an integer value from shared preference and convert it to an enum type.
My enum is:
enum ThemeColor { red, gree, blue, orange, pink, white, black };
I want to easily convert an integer to an enum:
final prefs = await SharedPreferences.getInstance(); ThemeColor c = ThemeColor.convert(prefs.getInt('theme_color')); // something like that