GETX主题管理
我正在尝试在我的应用程序中实现主题管理。
在我拥有的GetMaterialApp中:
themeMode: ThemeService().getThemeMode(),
在主题服务类中,我有:
ThemeMode getThemeMode() {
String theme = getSavedTheme();
print('Loading theme: $theme');
switch (theme) {
case 'dark':
return ThemeMode.dark;
case 'system':
return ThemeMode.system;
default:
return ThemeMode.light;
}
}
String getSavedTheme() {
var value = _getStorage.read(storageKey);
print('Loaded: $value');
return value ?? 'light';
}
第一次打开应用程序时,该应用程序总是在Light主题中加载,并且我在控制台中获得以下输出:
I/flutter ( 4252): Loaded: null
I/flutter ( 4252): Loading theme: light
似乎GetX存储没有加载值来自存储的prefs。手动检查文件时,该值在prefs中是正确的:
{"savedTheme":"system"}
如果我首次加载应用程序后立即重新启动,它可以正常工作并更改应用程序主题。
请问我可能做错了什么?
I am trying to implement theme management in my app.
In the GetMaterialApp I have:
themeMode: ThemeService().getThemeMode(),
In the ThemeService class I have:
ThemeMode getThemeMode() {
String theme = getSavedTheme();
print('Loading theme: $theme');
switch (theme) {
case 'dark':
return ThemeMode.dark;
case 'system':
return ThemeMode.system;
default:
return ThemeMode.light;
}
}
String getSavedTheme() {
var value = _getStorage.read(storageKey);
print('Loaded: $value');
return value ?? 'light';
}
When I open the app for the first time, the app always loads in light theme and I get the following output in the console:
I/flutter ( 4252): Loaded: null
I/flutter ( 4252): Loading theme: light
Seems like Getx Storage isn't loading the value from the stored prefs. Checking the file manually, the value is correct in the prefs:
{"savedTheme":"system"}
If I hot restart right after loading the app for the first time, it works properly and changes the app theme.
Any ideas on what I might be doing wrong, please?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请检查主要方法的GetStorage初始化
Please check GetStorage initialization in Main method
替换
解决我的问题的解决方案是为了完整的缘故
,这就是我在主题服务类中所拥有的
The solution to my problem was to replace
with
For completness sake, this is what I had in the ThemeService class