我试图保存我的应用程序的主题使用bool变量在获取存储。但每当我重新启动或关闭应用程序后打开应用程序,变量得到空。
void changeTheme() {
final box = GetStorage();
box.writeIfNull('isDark', false);
bool isDark = box.read('isDark');
isDark
? Get.changeTheme(QRCodeTheme.lightThemeData)
: Get.changeTheme(QRCodeTheme.darkThemeData);
box.write('isDark', !isDark);
}
void main() async {
WidgetsFlutterBinding.ensureInitialized();
Prefs.init();
runApp(QRCodeScannerApp());
}
class QRCodeScannerApp extends StatefulWidget {
QRCodeScannerApp({Key? key}) : super(key: key);
@override
State<QRCodeScannerApp> createState() => _QRCodeScannerAppState();
}
class _QRCodeScannerAppState extends State<QRCodeScannerApp> {
bool darkMode = false;
@override
void initState() {
final box = GetStorage();
box.writeIfNull('isDark', false);
darkMode = box.read('isDark');
super.initState();
}
@override
Widget build(BuildContext context) {
return GetMaterialApp(
debugShowCheckedModeBanner: false,
theme:
darkMode ? QRCodeTheme.darkThemeData : QRCodeTheme.lightThemeData,
initialRoute: AppRoutes.home,
getPages: [
GetPage(
name: AppRoutes.home,
page: () => HomePage(),
),
],
);
}
}
我错过了什么?提前感谢!
1条答案
按热度按时间cygmwpex1#
在您的
void main
中,请输入以下代码您应该添加
await
关键字P.S:-在你的情况下,你用写
await Prefs.init()