Is it possible to observe variables and/or collections in Dart without using the Polymer library?
Asked
Active
Viewed 804 times
2 Answers
4
Yes you can, using the observe package: http://pub.dartlang.org/packages/observe

Robert
- 5,484
- 1
- 22
- 35
-
This package has been `DISCONTINUED` guys, so possibly avoid – Billy Mahmood Feb 10 '20 at 15:39
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