Search code examples
dartdart-polymer

How to replace deprectaed notifyProperty?


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.


Solution

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