如何观察数据变化

时间:2019-12-17 11:04:06

标签: flutter dart

如何观察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;
    }

我在这里缺少什么?

1 个答案:

答案 0 :(得分:0)

您应该使用

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

而不是 addStream()

这是RxDart和Flutter中流的很好的教程: https://medium.com/flutter-community/reactive-programming-streams-bloc-6f0d2bd2d248