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

我一直在尝试向flutter应用程序添加redux

我想基于用户共享的首选项设置MaterialUi主题

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

  runApp(MyApp(store: store));
}

我有这个功能,返回什么用户首选项

  static Future<String> getActiveTheme() async {
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    return prefs.getString(_activeTheme) ?? "Light";
  }

我的AppState看起来像这样

class AppState {
  String theme;
  User user;

  AppState(this.theme, this.user);

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

我想要而不是theme: 'Light'得到theme: getActiveTheme()

提前致谢

汤姆·阿拉巴斯特

您要做的是将主题作为main方法的一部分加载然后,您可以将已加载的主题传递到AppState构造函数中。这并不是说我已经添加async到您的main方法,以及移动的设置theme在你AppState的参数。

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;
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章