我一直在尝试将 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";
}
我的应用状态看起来像这样
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;
}