gpt4 book ai didi

dart - 如何使用Dart ChangeNotifier类?

转载 作者:行者123 更新时间:2023-12-03 02:54:09 25 4
gpt4 key购买 nike

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

  • 在对记录的每次更新中,List<ChangeRecord>对象中报告的更改数将递增重复。见图片:

  • ChangeRecord不允许仅检索新值。因此,我正尝试使用MapChangeRecord。但是,我不知道如何使用它。

  • 这是我的示例代码供引用:
    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));
    }

    最佳答案

    每次执行此代码

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

    它应该按预期工作

    关于dart - 如何使用Dart ChangeNotifier类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20774215/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com