如何在Flutter中将流数据从一个类发送到另一个类

问题描述

我正在编写一个从类websocket.dart侦听WebSocket流的代码,我想将数据或流对象本身传递给另一个类。

websocket侦听器不断接收数据,因此它是异步且连续的。

我通过调用授权函数开始收听。

  Future<bool> authorize(String token) async {
    this._authorized = false;
    this._websocket.sink.add('{"topic":"AUTHORIZE","payload":"$token"}');
    this._websocket.stream.listen(this.receiveMessage);

    // wait 3s for connection
    await Future.delayed(new Duration(seconds: 3));

    if (this._authorized) return true;

    return false;
  }

  receiveMessage(message) => this.parseMessage(json.decode(message));

  parseMessage(Map message) {
    var payload = message['payload'];

    if (message.containsValue("AUTHORIZE_RESP")) {
      int code = payload['code'];
      // code ... 0 sucessful
      // code ... 1 failed
      if (code == 0)
        this._authorized = true;
      else
        this._authorized = false;
    } else if (message.containsValue("POSITIONS")) {
      this._positionMsg += 1;
      setRefresh(_deviceMsg,_positionMsg);
    } else if (message.containsValue("DEVICES")) {
      Position positionMessage = positionFromJson(message);
      this._deviceMsg += 1;
      setRefresh(_deviceMsg,_positionMsg);
    }
  }

我想传递 Position 对象或所接收的消息本身(如果更易于实现)。直到最后,数据都将被处理,并且与小部件无关。 diagram for the project hierarchy

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...