0

Is it possible to observe variables and/or collections in Dart without using the Polymer library?

Tomen
  • 4,854
  • 5
  • 28
  • 39

2 Answers2

4

Yes you can, using the observe package: http://pub.dartlang.org/packages/observe

Robert
  • 5,484
  • 1
  • 22
  • 35
3

An example I built a while ago seems still to 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