[英]Flutter redux How to perform polling
Redux
似乎是用於移動應用程序開發的非常好的架構。 但是,移動應用程序具有一些Web應用程序不常見的功能。
就我而言,我想在某個特定屏幕開始后啟動長時間輪詢/監視位置/跟蹤文件系統(監視外部狀態的任何操作),並在屏幕關閉時停止。
假設我們有一個函數,隨着時間的推移會發出多個事件。
Stream<Event>monitor();
我只想在某些特定屏幕處於活動狀態時收聽此功能。
最好的方法是什么?
假設您有3個頁面:“ PageHome.dart”,“ Page1.dart”,“ Page2.dart”。
創建另一個dart文件“ GlobalVariables.dart”,在該文件內創建一個類gv,為這三個頁面創建靜態redux“商店”。
在gv中創建靜態var strCurPage。
假設每個頁面都有一個變量,該變量將由外部事件更改,請將它們在gv中聲明為static var。
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';
}
在所有其他dart文件中導入“ GlobalVariables.dart”。
在導航到新頁面(例如,從PageHome到Page1)之前,請設置:
gv.strCurPage ='Page1';
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
}
}
我不知道這是否是最好的方法,但是使用redux和GlobalVariables.dart,我可以:
知道用戶當前正在瀏覽哪個頁面。
即使事件觸發時用戶不在該頁面上,也要更改頁面的內容。 (但是稍后用戶導航到該頁面時,將顯示內容)
事件觸發時,無論用戶導航到哪個頁面,都強制用戶轉到特定頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.