首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Dart类?

如何使用Dart类?
EN

Stack Overflow用户
提问于 2013-12-25 15:20:02
回答 1查看 2.8K关注 0票数 2

我正在探索Dart的观察库中的ChangeNotifier类,以便在命令行应用程序中使用。但是我有两个问题。

  1. List<ChangeRecord>对象中报告的更改的数量在记录的每次更新中递增地重复。见图:

  1. ChangeRecord不允许只检索新值。因此,我尝试使用MapChangeRecord代替。但是,我不知道怎么用它。

这是我的示例代码供参考:

代码语言:javascript
复制
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));
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-12-25 16:39:18

每次执行此代码时

代码语言:javascript
复制
stdinStream
    .transform(new Utf8Decoder())
      .listen((e) => notifiable.change(e));

notifiable.change(e)中添加新的订阅

如果你像这样改变它

代码语言:javascript
复制
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));
}

它应该能像预期的那样工作。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20774215

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档