首页 > 解决方案 > 我试图通过 Spring 服务器发送的事件获取我的数据,当使用 streambuilder 快照时我得到空值?

问题描述

我正在尝试使用服务器发送的事件从后端获取我的数据,当我在流上监听时,我得到了我的数据,但是当我使用 streamBuilder 快照时,我得到 null,为什么会这样。

春季事件发射器

private void sendInitEvent(SseEmitter sseEmitter){
    Timer timer = new Timer();
    Location location = new Location();
    location.setId(1);
    location.setLat(12.2);
    location.setLon(14.3);
    timer.schedule(new TimerTask() {
        @Override
        public void run() {
            try {


                sseEmitter.send(SseEmitter.event().data(location));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }, 0, 10000);

}

颤振使用听

void listen() {
   streamedResponse!.stream.listen((event) {
     print(utf8.decode(event));
   });
 }

上述代码的输出

I/flutter ( 7409): data:
I/flutter ( 7409): {"id":1,"lat":12.2,"lon":14.3}

上述代码的输出没有 utf8 解码

I/flutter ( 8103): [100, 97, 116, 97, 58]
I/flutter ( 8103): [123, 34, 105, 100, 34, 58, 49, 44, 34, 108, 97, 116, 34, 58, 49, 
                    50, 46, 50, 44, 34, 108, 111, 110, 34, 58, 49, 52, 46, 51, 125]
I/flutter ( 8103): [10, 10]

使用 StreamBuilder 颤振

StreamBuilder(
  builder: (context, snapshot) {
    if (snapshot.hasData) {
      return Center(
        child: CircularProgressIndicator(
          backgroundColor: Colors.lightBlueAccent,
        ),
      );
    }
    if (snapshot.hasError) {
      print(snapshot.error);
    }
    print(snapshot.data);
    return Container(
      color: Colors.white,
      child: Text("Stream Builder"),
    );
  },
  stream: streamedResponse!.stream,
);

上述代码的输出

I/flutter ( 8103): [10, 10]

标签: androidspring-bootflutterdartmobile

解决方案


推荐阅读