2

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.

Fox32
  • 13,126
  • 9
  • 50
  • 71

1 Answers1

4

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);
  }
}
Matt B
  • 6,192
  • 1
  • 20
  • 27
  • How would you keep the `oldValue` around when dealing with Lists? – Marco Jakob Oct 28 '13 at 15:55
  • 1
    Whatever is passed as the last value of notifyPropertyChange will be returned by the function. If you pass the full list (instead of the changed index) you can continue with this method. But instead of using `onPropertyChange` you will need to do: `myList.changes.listen((_) { _oldValue = notifyPropertyChange(#xyz, _oldValue, xyz); });` – Matt B Oct 28 '13 at 17:10