目前,我有一个计算的 setter/getter xyz。要安排新的计算,我调用notifyProperty(this, #xyz);
。
在最新版本的watch中,不建议使用notifyProperty
。我该如何更换?该文档建议使用this.notifyPropertyChange(#xyz, oldValue, newValue);
。问题是,在计算 setter/getter 时,我没有oldValue
(也不直接是newValue
)。
最佳答案
suggestion from the devs将oldValue
保留在私有(private)变量中以供引用。至于newValue
,您实际上可以只传递getter,它将使用新值进行计算。
您将正在学习类似于以下内容的类(class):
class MyElement extends Observable {
@observable var foo, bar;
var _oldValue;
@reflectable get xyz => foo + bar;
MyElement() {
// we use the xyz getter to compute its own new value
// notifyPropertyChange returns the new value for convenience :)
notifyXyz() { _oldValue = notifyPropertyChange(#xyz, _oldValue, xyz); }
onPropertyChange(this, #foo, notifyXyz);
onPropertyChange(this, #bar, notifyXyz);
}
}
关于dart - 如何替换已弃用的通知属性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19549049/