我想要将自定义主题应用到我的应用程序。因此,在创建“MaterialApp”小部件之前,我要检查SharedPreferences中的当前主题。为此,我使用了Future Builder。
class MyApp extends StatelessWidget {
ThemeData myTheme;
bool isDark;
@override
Widget build(BuildContext context) {
isDark=false;
return FutureBuilder(
future: getTheme(),
builder: (context,snapshot) {
if (snapshot.hasData) {
if(isDark)
myTheme=Constants.darkTheme;
else
myTheme=Constants.lightTheme;
return MaterialApp(
title: 'Sample app',
debugShowCheckedModeBanner: false,
theme: myTheme,
home: HomePage(),
);
}
return MaterialApp(
title: 'Sample app',
debugShowCheckedModeBanner: false,
home: Scaffold(body: Center(child: Text('Loading')),),
);
},
);
}
Future<bool> getTheme() async {
SharedPreferences sharedPreferences = await SharedPreferences.getInstance() ;
isDark=sharedPreferences.getBool("isDark");
if(isDark==null) {
isDark=false;
sharedPreferences.setBool("isDark", isDark);
}
return isDark;
}
}除了这个错误之外,所有的工作都很好。此外,应用程序也不会崩溃。
E/flutter ( 5809): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: 'package:flutter/src/animation/animation_controller.dart': Failed assertion: line 741 pos 7: '_ticker != null': AnimationController.stop() called after AnimationController.dispose()
E/flutter ( 5809): AnimationController methods should not be used after calling dispose.
E/flutter ( 5809): #0 _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:46:39)
E/flutter ( 5809): #1 _AssertionError._throwNew (dart:core-patch/errors_patch.dart:36:5)
E/flutter ( 5809): #2 AnimationController.stop (package:flutter/src/animation/animation_controller.dart:741:7)
E/flutter ( 5809): #3 AnimationController.repeat (package:flutter/src/animation/animation_controller.dart:637:5)
E/flutter ( 5809): #4 BallPulseIndicator.startAnim (package:loading/indicator/ball_pulse_indicator.dart:48:16)
E/flutter ( 5809): #5 BallPulseIndicator.startAnims.<anonymous closure> (package:loading/indicator/ball_pulse_indicator.dart:55:9)
E/flutter ( 5809): #6 new Future.delayed.<anonymous closure> (dart:async/future.dart:326:39)
E/flutter ( 5809): #7 _rootRun (dart:async/zone.dart:1182:47)
E/flutter ( 5809): #8 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 5809): #9 _CustomZone.runGuarded (dart:async/zone.dart:997:7)
E/flutter ( 5809): #10 _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1037:23)
E/flutter ( 5809): #11 _rootRun (dart:async/zone.dart:1190:13)
E/flutter ( 5809): #12 _CustomZone.run (dart:async/zone.dart:1093:19)
E/flutter ( 5809): #13 _CustomZone.bindCallback.<anonymous closure> (dart:async/zone.dart:1021:23)
E/flutter ( 5809): #14 Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
E/flutter ( 5809): #15 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:397:19)
E/flutter ( 5809): #16 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:428:5)
E/flutter ( 5809): #17 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)如何修复此错误?
https://stackoverflow.com/questions/64928554
复制相似问题