How to replace deprectaed notifyProperty?

假装没事ソ 提交于 2019-12-10 11:14:35

问题


Currently I have a getter xyz that is computed. To schedule a new computation I call notifyProperty(this, #xyz);.

In the the latest version of observe, notifyProperty is deprecated. How can I replace it? The documentation suggests to use this.notifyPropertyChange(#xyz, oldValue, newValue);. The problem is, that I don't have the oldValue (and not directly the newValue) as the getter is computed.


回答1:


The suggestion from the devs is to keep the oldValue around in a private variable for reference. As for the newValue you can actually just pass the getter and it will compute it with the new values.

You will be looking at a class similar to this:

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);
  }
}


来源:https://stackoverflow.com/questions/19549049/how-to-replace-deprectaed-notifyproperty

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!