首页 > 解决方案 > 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()此示例中也是如此。但我不知道如何解决这个问题。任何想法?谢谢。

标签: flutterredux

解决方案


我没有得到答案,所以我试图做的是更新我的商店的任何“其他”值,以触发我的 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

      },
    ),

推荐阅读