【发布时间】:2020-04-14 09:48:24
【问题描述】:
我有这样的课:
class A extends B {
final Property<bool> property = Property<bool>(this);
}
class Property<T> {
Property(this.b);
final B b;
}
但我在this 上收到错误消息:
对“this”表达式的引用无效。
我相信我当时无法访问this,可能是因为对象引用还没有准备好。
所以我尝试了其他形式的初始化该变量,例如:
class A extends B {
final Property<bool> property;
A() : property = Property<bool>(this);
}
但我得到了同样的错误。
唯一有效的是:
class A extends B {
Property<bool> property;
A() {
property = Property<bool>(this);
}
}
这需要我删除 final 变量声明,这是我不想这样做的。
如何在 Dart 中初始化一个需要引用对象本身的 final 变量?
【问题讨论】: