2

I am trying to use the observe package without having to have annotations in my Model, and only by raising notifyPropertyChange in setters, so to test i made the following test

import 'package:observe/observe.dart';
import 'dart:async';
import 'dart:math';
void main() {

  var dummyWatchingModel = new DummyWatchingModel();
  new Timer.periodic(new Duration(milliseconds:1000), (_){
           //calls a function that set a random value to the property in the observable model
           dummyWatchingModel.setModelProps();
      });
}

class Model extends Observable{
  int _x;
  Model(this._x);

  int get x=> _x;
  void set x(int value){
    _x = notifyPropertyChange(#_x, _x, value);
  }
}

class DummyWatchingModel{
  Model model = new Model(1); 
  final rng = new Random(); 
  anotherModel(){

    //watch for changes in model instance properties
    this.model.changes.listen((List<ChangeRecord> records) {
      for(ChangeRecord change in records){
        print(change.toString());
      }
    });
  }

  //the callback for the timer to assign a random value model.x
  setModelProps(){
    model.x = rng.nextInt(100);
    print('called...');
  }
}

i am changing the value of a property in an instance of Model using a setter that raises notifyPropertyChange but it never listens for changes, any idea why?

FPGA
  • 3,525
  • 10
  • 44
  • 73

1 Answers1

2

I think you want to use ChangeNotifier instead of Observable.

I'm not sure about notifyPropertyChange but with Observable you normally need to call dirtyCheck to get notified about changes.

I made a small example a while ago to learn how these two work:

import 'package:observe/observe.dart';

class Notifiable extends Object with ChangeNotifier {
  String _input = '';

  @reflectable
  get input => _input;

  @reflectable
  set input(val) {
    _input = notifyPropertyChange(#input, _input, val + " new");
  }

  Notifiable() {
    this.changes.listen((List<ChangeRecord> record) => record.forEach(print));
  }
}

class MyObservable extends Observable {
  @observable
  String counter = '';

  MyObservable() {
    this.changes.listen((List<ChangeRecord> record) => record.forEach(print));
  }
}

void main() {
  var x = new MyObservable();
  x.counter = "hallo";
  Observable.dirtyCheck();

  Notifiable notifiable = new Notifiable();
  notifiable.input = 'xxx';
  notifiable.input = 'yyy';
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
  • got it to work without using @reflectable but only when listening to changes from inside the model itself, if i try to listen to changes of the model from an instance of DummyWatchingModel it never works, try listening to changes from outside the model itself – FPGA Jun 18 '14 at 04:33
  • 1
    @reflectable might only be needed when you build to JavaScript only so these fields don't get dropped by treeshaking. I'll take a look if I get it work. – Günter Zöchbauer Jun 18 '14 at 04:35
  • updated with the listen statement in different positions, th one inside DummyWatchingModel never works, but the ones in main or inside the ctor of the observed model work fine – FPGA Jun 18 '14 at 04:51
  • never mind the constructor name was wrong works fine now haha – FPGA Jun 18 '14 at 05:05