Observe package from polymer dart

后端 未结 1 408
忘掉有多难
忘掉有多难 2020-12-06 23:07

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 t

相关标签:
1条回答
  • 2020-12-06 23:28

    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';
    }
    
    0 讨论(0)
提交回复
热议问题