我一直在尝试向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] 删除。
我来说两句