flutter - Flutter Redux Dispatch Action 更新状态,但不更新 Stream 中的 UI
问题描述
我有一个stream
调用 API 来接收数据的方法,每次接收数据时,监听dispatch
器都会action
更新 reduxstore
状态(项目列表),状态会成功更新。
一旦状态更新为来自的数据,此项目列表应更新 UI Stream
。那不会发生。
这是一个代码示例:
我的流(流类的一部分):
LoadLastDataStream () {
Timer.periodic(Duration(seconds: 2), (t) async {
_controller.sink.add(dataList); //StreamController
UsersService.loadLastData().then((value) => dataList = value);
});
}
stream
以下代码正在收听上述内容,该代码会更新状态,但不会更新 UI:
myStream.listen(
(dataList){
if(dataList.length > 0)
store.dispatch(new SaveLastDataListAction(dataList));//called successfully
},
),
我认为这与频繁更新状态不会更新 UI 的事实有关,即使setSteate()
在此示例中也是如此。但我不知道如何解决这个问题。任何想法?谢谢。
解决方案
我没有得到答案,所以我试图做的是更新我的商店的任何“其他”值,以触发我的 ViewModel 状态已经改变。
因此,例如,我必须添加以下行:
myStream.listen(
(dataList){
if(dataList.length > 0)
store.dispatch(new SaveLastDataListAction(dataList));//called successfully
//I had to add this line to trigger my VM
store.dispatch(updateOtherValueInMyReducer('any value'));
//Only the above line is enough for me
},
),
推荐阅读
- sql - 使用 where 在 ruby 中搜索一个特定的 id,但返回多个不同的
- python - 从字典中删除键值对
- javascript - 我们可以将多个 updateOne 转换为单个查询吗?
- c# - 如何正确写入从 Web API 接收的文件
- javascript - 由于糟糕的 JS,Rails 资产预编译失败
- c# - 如何使用 HostBuilder Generic Host ref StopAsync OperationCanceledException 设置 ShutdownTimeout
- c# - C# ExecuteScalar()
- c# - 如何使用 IsolatedStorageFile.GetUserStoreForApplication()
- python - 我可以打印 object.__doc__ 但我无法将其保存到文件中
- c++ - Protobufs 构建但未使用 CMake 链接到主程序