我正在探索Dart的观察库中的ChangeNotifier类,以便在命令行应用程序中使用。但是我有两个问题。
List<ChangeRecord>对象中报告的更改的数量在记录的每次更新中递增地重复。见图:
这是我的示例代码供参考:
import 'dart:io';
import 'dart:async';
import 'dart:convert';
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);
}
void change(String text) {
input = text;
this.changes.listen((List<ChangeRecord> record) => print(record.last));
}
}
void main() {
Notifiable notifiable = new Notifiable();
Stream stdinStream = stdin;
stdinStream
.transform(new Utf8Decoder())
.listen((e) => notifiable.change(e));
}发布于 2013-12-25 16:39:18
每次执行此代码时
stdinStream
.transform(new Utf8Decoder())
.listen((e) => notifiable.change(e));在notifiable.change(e)中添加新的订阅
如果你像这样改变它
import 'dart:io';
import 'dart:async';
import 'dart:convert';
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);
}
Notifiable() {
this.changes.listen((List<ChangeRecord> record) => print(record.last));
}
void change(String text) {
input = text;
}
}
void main() {
Notifiable notifiable = new Notifiable();
Stream stdinStream = stdin;
stdinStream
.transform(new Utf8Decoder())
.listen((e) => notifiable.change(e));
}它应该能像预期的那样工作。
https://stackoverflow.com/questions/20774215
复制相似问题