观察者模式-我可以在没有聚合物的情况下观察Dart中的变量吗?



是否可以在不使用聚合物库的情况下观察Dart中的变量和/或集合?

可以,使用observe包:http://pub.dartlang.org/packages/observe

我不久前构建的一个示例似乎仍然有效

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

最新更新