获取Dart类的名称作为Type或String [英] Get the name of a Dart class as a Type or String
问题描述
是否可以通过 String
或 Type
对象获取 dart类
的类名.>
Is there a way to get the class name of a dart class
as a String
or a Type
object..?
class MyClass {
}
var myClass = MyClass();
我知道 property
, runtimeType
,它们将对象的类型作为 Type
对象返回.但是类有类似的功能吗?
I know the property
, runtimeType
which return the type of the object as a Type
object. But is there a similar function for classes?
print(myClass.runtimeType.toString());
我当前要做的是创建该类的对象,并使用 runtimeType
.
What I currently do is creating an object of the class and use runtimeType
.
String type = MyClass().runtimeType.toString();
注意:在 python
中,每个类中都有一个名为 __ name __
的变量,它可以满足我的需要.
Note: In python
there is a variable called __name__
in every class, which does what I need.
我的最终目标是使用以前保存的类名称创建dart对象.在此问题中,他们提出了一种使用 Map
s的方法.
问题是我有很多类,而且这种方法在我的情况下看起来很混乱.
My final goal is to create dart objects using previously saved class names. In this issue they have proposed a method using Map
s.
The thing is that I have lots of classes and that method looks messy in my situation.
我目前要做的是,通过以下方式保存对象类型:
What I currently do is, save the object type by:
var saving = myClass.runtimeType.toString();
加载时:
if (saving == MyClass().runtimeType.toString()) {
return MyClass();
}
根据您的经验和意见,您可以提出更好的解决方案吗?
From your experiences and opinions, can you propose a better solution?
推荐答案
类类型可用作 Type
:
Type myType = MyClass;
这篇关于获取Dart类的名称作为Type或String的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!