gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 06:57:41 27 4
gpt4 key购买 nike

我一直在尝试将 redux 添加到我的 flutter 应用中

我想根据用户共享的偏好设置 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 方法,并将 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;
}

关于redux - 如何从共享首选项设置 initialState?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55361805/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com