如何在flutter中通过字符串名称访问类的元素 [英] How to access to element of a class by string name in flutter
本文介绍了如何在flutter中通过字符串名称访问类的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从api获得了很棒的字体名称,我想访问font_awesome_flutter包的元素,但是我在C#中知道它,但在flutter中却不知道:从服务器收到:
I get name of font awesome from api that I want to access element of font_awesome_flutter package but I know it in C# but I do not know in flutter: received from server:
{
"fontawesomeName":"breadSlice"
}
在font_awesome包中,我可以通过以下方式访问其元素
in font_awesome package I can access its element by below
IconButton(
icon: FaIcon(FontAwesomeIcons.breadSlice),
onPressed: () { print("Pressed"); }
)
但是如何在flutter中访问具有字符串名称的对象的元素?
But How do I access element of object with string name in flutter?
推荐答案
在Flutter中这是不可能的,因为已禁用反射.唯一的方法是创建映射.例如:
This is not possible in Flutter because reflection is disabled. The only way is to create a mapping. For example:
const Map<String, IconData> map = {
"breadSlice": FontAwesomeIcons.breadSlice,
};
IconData getIcon(String iconName) {
return map[iconName];
}
这篇关于如何在flutter中通过字符串名称访问类的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文