Flutter/Dart将Int转换为Enum [英] Flutter / Dart Convert Int to Enum

查看:180
本文介绍了Flutter/Dart将Int转换为Enum的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有简单的方法可以将整数值转换为枚举?我想从共享首选项中检索一个整数值,并将其转换为枚举类型.

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.

我的列举是:

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

推荐答案

int idx = 2;
print(ThemeColor.values[idx]);

应该给您

ThemeColor.blue

这篇关于Flutter/Dart将Int转换为Enum的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆