flatter redux如何执行轮询

flatter redux如何执行轮询,redux,dart,flutter,Redux,Dart,Flutter,Redux似乎是一个非常好的移动应用程序开发架构。但是,移动应用程序具有一些web应用程序不常见的功能 在我的例子中,我希望在某个特定屏幕启动后启动长轮询/监视位置/跟踪文件系统(监视某些外部状态的任何操作),并在屏幕关闭时停止 假设我们有一个函数,它可以随时间发出多个事件 Streammonitor(); 我只想在某些特定屏幕处于活动状态时收听该功能 最好的方法是什么 假设您有3个页面:“PageHome.dart”、“Page1.dart”、“Page2.dart” 创建另一个dart文件

Redux
似乎是一个非常好的移动应用程序开发架构。但是,移动应用程序具有一些web应用程序不常见的功能

在我的例子中,我希望在某个特定屏幕启动后启动长轮询/监视位置/跟踪文件系统(监视某些外部状态的任何操作),并在屏幕关闭时停止

假设我们有一个函数,它可以随时间发出多个事件

Streammonitor();
我只想在某些特定屏幕处于活动状态时收听该功能


最好的方法是什么

假设您有3个页面:“PageHome.dart”、“Page1.dart”、“Page2.dart”

  • 创建另一个dart文件“GlobalVariables.dart”,在此文件中创建一个类gv,为三个页面创建静态redux“stores”

  • 在gv中创建静态var strurpage

  • 假设每个页面都有一个将由外部事件更改的变量,那么在gv中将它们声明为静态变量

  • GlobalVariables.dart中的代码:

    
    import 'package:redux/redux.dart';
    
    enum Actions {
      Increment
    } 
    
    
    // The reducer, which takes the previous count and increments it in response to an Increment action.
    int reducerRedux(int intSomeInteger, dynamic action) {
      if (action == Actions.Increment) {
        return intSomeInteger + 1;
      }
      return intSomeInteger;
    }
    
    class gv {
      static Store<int> storePageHome =
        new Store<int>(reducerRedux, initialState: 0);
      static Store<int> storePage1 =
        new Store<int>(reducerRedux, initialState: 0);
      static Store<int> storePage2 =
        new Store<int>(reducerRedux, initialState: 0);
      static String strCurPage = 'PageHome';
      static String strPageHomeVar = 'PageHomeInitialContent';
      static String strPage1Var = 'Page1InitialContent';
      static String strPage2Var = 'Page2InitialContent';
    }
    
    
    我不知道这是否是最好的方法,但使用redux和GlobalVariables.dart,我可以:

  • 了解用户当前正在浏览的页面

  • 即使触发事件时用户不在页面上,也要更改页面内容。(但当用户稍后导航到该页面时,将显示内容)

  • 触发事件时,无论用户正在导航哪个页面,都强制用户转到特定页面

  • void thisFunctionCalledByExternalEvent(strPage1NewContent, strPage2NewContent) {
      gv.strPage1Var = strPage1NewContent;
      gv.strPage2Var = strPage2NewContent;
    
      if (gv.strCurPage == 'Page1') {
        // Dispatch storePage1 to refresh Page 1
        storePage1.dispatch(Actions.Increment);
      } else if (gv.strCurPage == 'Page2') {
        // Dispatch storePage2 to refresh Page 2
        storePage2.dispatch(Actions.Increment);
      } else {
        // Do not need to refresh page if user currently navigating other pages.
        // so do nothing here
      }
    }