获取有史以来经过的所有价值

发布于 2025-02-14 01:28:35 字数 497 浏览 1 评论 0原文

假设我们要创建一个流的流,该流将包含映射< String,String>。现在,这里的收获是我想创建一个函数,该函数将在此MAP< String,String>中添加新的键值对。

例如,

final _configMap = BehaviorSubject<Map<String, dynamic>>();
Stream<Map<String, dynamic>> get configMap => _configMap.stream;
Function(Map<String, dynamic>) get addConfigMap => _configMap.sink.add;

现在调用接收器AddConfigmap只能接受新的地图。 但我的要求是创建一个行为主体,该行为会接受键值对,并且其流将散发出包含我们传递给它的所有键值对的地图。是否有可能?如果是,那怎么办?

Let's say that we want to create a stream which would contain a Map<String, String>. Now the catch here is that I want to create a function that would add new key-value pairs into this stream of Map<String, String>.

e.g.,

final _configMap = BehaviorSubject<Map<String, dynamic>>();
Stream<Map<String, dynamic>> get configMap => _configMap.stream;
Function(Map<String, dynamic>) get addConfigMap => _configMap.sink.add;

Now calling the sink addConfigMap will only accept a new map. But my requirement here is to create a BehaviorSubject that would accept Key-Value pairs and whose stream will emit a Map containing all those key-value pairs that we have ever passed into it. Is it possible ? If yes, then how ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

您可以将“累加器”映射存储在类上,然后在流的输入元素上映射,将新元素插入存储的map ,然后最终发射存储的map

class Foo {
  final _configMap = BehaviorSubject<Map<String, dynamic>>();

  Map<String, dynamic> _map = {}; // Store the map

  Stream<Map<String, dynamic>> get configMap => _configMap.stream.map((newMap) {
        _map.addAll(newMap);
        return _map;
      });

  Function(Map<String, dynamic>) get addConfigMap => _configMap.sink.add;
}

使用:

void main() async {
  final foo = Foo();

  foo.configMap.listen((map) {
    print(map);
  });

  foo.addConfigMap({"test-1": 1}); // Prints {"test-1": 1}
  foo.addConfigMap({"test-2": 2}); // Prints {"test-1": 1, "test-2": 2}
  foo.addConfigMap({"test-3": 3}); // Prints {"test-1": 1, "test-2": 2, "test-3": 3}
}

You can store the "accumulator" map on the class, then map over the input elements of your stream, inserting the new elements into the stored Map, and finally emitting the stored Map:

class Foo {
  final _configMap = BehaviorSubject<Map<String, dynamic>>();

  Map<String, dynamic> _map = {}; // Store the map

  Stream<Map<String, dynamic>> get configMap => _configMap.stream.map((newMap) {
        _map.addAll(newMap);
        return _map;
      });

  Function(Map<String, dynamic>) get addConfigMap => _configMap.sink.add;
}

To use:

void main() async {
  final foo = Foo();

  foo.configMap.listen((map) {
    print(map);
  });

  foo.addConfigMap({"test-1": 1}); // Prints {"test-1": 1}
  foo.addConfigMap({"test-2": 2}); // Prints {"test-1": 1, "test-2": 2}
  foo.addConfigMap({"test-3": 3}); // Prints {"test-1": 1, "test-2": 2, "test-3": 3}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文