如何观察数据变化



如何观察dart中的数据变化?

假设我有一个Map<String, String>,我想观察它的数据变化。

我当前的代码状态:-

我有StreamController携带Map<String, String>类型:- `

static StreamController<Map<String, String>> notificationReceive = new StreamController<Map<String, String>>();

我让用户调用X方法来监听这些流,即:

static StreamController<Map<dynamic, dynamic>> listenForNotificationReceive()  {
_channel.invokeMapMethod("receiveCallback");
return notificationReceive;
}

然后在另一个地方,我调用这个函数,如:-

AnotherClass.listenForNotificationReceive().stream.listen((onData) {
print("MAIN, received: " + onData.toString());
});

但它从未被召唤过。

当从本机端调用某些方法时,我会推送数据:-

switch (call.method) {
case "received":
print("File.dart: "+  call.arguments.toString());
notificationReceive.addStream(call.arguments);
break;
}

我在这里错过了什么?

你应该使用

notificationReceive.sink.add(call.arguments); // where call.arguments is the new map

而不是addStream((

以下是 RxDart 和颤振流的好教程: https://medium.com/flutter-community/reactive-programming-streams-bloc-6f0d2bd2d248

最新更新