我有一个类:
class Dog {
Dog() {
print("Current classname is: ${this.className}");
}
}
看起来这个.className不被允许。有相应的替代方案吗?
我有一个类:
class Dog {
Dog() {
print("Current classname is: ${this.className}");
}
}
看起来这个.className不被允许。有相应的替代方案吗?
this.runtimeType
或者只是
runtimeType
返回当前类的名称。
如果你想在类的开头而不是build方法中初始化一个变量,那么可以简单地按照以下方式操作:
String className = (YourClass).toString();
void printArea(Shape shape){
String shapeClass = (shape).runtimeType.toString();
print('The area of the $shapeClass is: ${shape.area}');
}
您可以使用以下代码打印类名:
print('$runtimeType');
this.runtimeType
如果您正在处理有状态的小部件,并且想要知道页面/小部件名称,您还可以使用以下内容来获取小部件名称。
widget.toString()
我们使用它来追踪最多点击的页面等等。