android - 我试图通过 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]
解决方案
推荐阅读
- google-apps-script - 根据 Google 表单中的下拉菜单选项自动发送电子邮件
- c++ - 通过作为指向 C++ 中的函数的指针传递来访问 std::array 元素的正确方法是什么?
- vba - 为什么 SumProduct 不能在 Excel VBA 中使用
- fortran - 我的 Fortran 程序无法打开同一目录下的 .ini 设置文件并告知没有此类文件
- c# - 不能在循环中画线
- xamarin - 如何计算视频的大小?
- flutter - 正确的页面导航
- androidx - Alpha-08:应用程序不能安排超过 100 个不同的作业
- mysql - 为什么在查询语句中添加 group by 后结果顺序发生了变化?
- react-native - 登录后更改主页的文本语言