我是一位尝试学习抖动的Android开发人员。我被困在检查对象是否是类的实例(有状态或无状态小部件)。
在Java中,我们使用like
if (object instanceOf MyClass) {
// object is an instance of MyClass
} else {
// object is not an instance of MyClass
}
Run Code Online (Sandbox Code Playgroud)
但是我不知道该如何做。
到目前为止,我已经尝试过
if (object is MyClass) {
// object is an instance of MyClass
} else {
// object is not an instance of MyClass
}
Run Code Online (Sandbox Code Playgroud)
但这总是错误的。
我已经看到了另一种可能的实现方式,new isInstanceOf<MyClass>()该方式可以在package:matcher/matcher.dart软件包中找到,但我不知道如何正确实现。
任何帮助都会很棒。提前致谢。
小智 8
尝试使用调试对象的类:
debugprint("$<object name>");
Run Code Online (Sandbox Code Playgroud)
然后手动匹配类类型。
这意味着您将找到is操作员所需的类。
object is <object's class>
Run Code Online (Sandbox Code Playgroud)
is works perfect with Widget classes. For example I have a widget
class AccountCreationPage extends StatefulWidget {...}
Run Code Online (Sandbox Code Playgroud)
Then I can check that my variable of type Widget is of AccountCreationPage class (gives true, if it is really this class):
_loginPage is AccountCreationPage ? 'Creation' : ""
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7067 次 |
| 最近记录: |