如何使用Dart ChangeNotifier类? [英] How to use Dart ChangeNotifier class?

查看:1009
本文介绍了如何使用Dart ChangeNotifier类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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


  1. List< ChangeRecord> ; 对象在每次更新记录时递增重复。查看图片:



    p>


  2. ChangeRecord不允许仅检索新值。因此,我试图使用MapChangeRecord代替。


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

  import'dart:io' 
import'dart:async';
import'dart:convert';
import'package:observe / observe.dart';

类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));每次执行此代码时,
}


解决方案

  stdinStream 
.transform(new Utf8Decoder())
。 ((e)=> notifiable.change(e));

您在 notifiable.change(e)



如果您更改为

  import'dart:io'; 
import'dart:async';
import'dart:convert';
import'package:observe / observe.dart';

类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));
}

它应该能正常工作


I'm exploring the ChangeNotifier class in Dart's observe library for use in a commandline application. But, I'm having two issues.

  1. The number of reported changes in a List<ChangeRecord> object are incrementally repeated in each update to the record. See image:

  2. ChangeRecord doesn't allow for retrieving only new values. Thus, I'm trying to use a MapChangeRecord instead. But, I don't know how to use it.

This is my sample code for reference:

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

解决方案

each time this code is executed

stdinStream
    .transform(new Utf8Decoder())
      .listen((e) => notifiable.change(e));

you add a new subscription in notifiable.change(e)

If you change it like

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

it should work as expected

这篇关于如何使用Dart ChangeNotifier类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆