Redux 如何从共享首选项设置initialState?

Redux 如何从共享首选项设置initialState?,redux,dart,flutter,sharedpreferences,Redux,Dart,Flutter,Sharedpreferences,我一直在尝试将redux添加到我的Flitter应用程序中 我想根据用户共享的首选项设置MaterialUi主题 void main() { final store = Store<AppState>( reducer, initialState: AppState.initialState(), ); runApp(MyApp(store: store)); } 我希望获得theme:getActiveTheme() 提前感谢您要做的是将主题作为ma

我一直在尝试将redux添加到我的Flitter应用程序中

我想根据用户共享的首选项设置MaterialUi主题

void main() {
  final store = Store<AppState>(
    reducer,
    initialState: AppState.initialState(),
  );

  runApp(MyApp(store: store));
}
我希望获得
theme:getActiveTheme()


提前感谢

您要做的是将主题作为
main
方法的一部分加载。然后可以将加载的主题传递到
AppState
构造函数中。我并没有将
async
添加到
main
方法中,并将
AppState
上的
theme
设置移动到参数中

void main() async {

  var theme = await getActiveTheme();

  final store = Store<AppState>(
    reducer,
    initialState: AppState.initialState(theme),
  );

  runApp(MyApp(store: store));
}

class AppState {

  // ...

  AppState.initialState(this.theme)
      : user = null;
}
void main()异步{
var-theme=await-getActiveTheme();
最终存储=存储(
减速器,
initialState:AppState.initialState(主题),
);
runApp(MyApp(商店:商店));
}
类AppState{
// ...
AppState.initialState(this.theme)
:user=null;
}

您应该考虑一个初始化状态,在这里可以获取这个“代码>字符串”并等待它,然后将其传递到AppStand。
class AppState {
  String theme;
  User user;

  AppState(this.theme, this.user);

  AppState.initialState()
      : theme = 'Light',
        user = null;
}

void main() async {

  var theme = await getActiveTheme();

  final store = Store<AppState>(
    reducer,
    initialState: AppState.initialState(theme),
  );

  runApp(MyApp(store: store));
}

class AppState {

  // ...

  AppState.initialState(this.theme)
      : user = null;
}