how to store starting and ending time of a timer using shared preference in flutter and when i close the app and reopen app should start from ending timer time
if anyone knows anything regarding this issue please share
how to store starting and ending time of a timer using shared preference in flutter and when i close the app and reopen app should start from ending timer time
if anyone knows anything regarding this issue please share
By using WidgetsBindingObserver
you can listen to your application state.
class MyPage extends StatefulWidget {
@override
_MyPageState createState() => _MyPageState();
}
class _MyPageState extends State<MyPage> with WidgetsBindingObserver {
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
}
@override
void dispose() {
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state);
// Here you will get your application state like resumed, inactive, paused, detached
// when the application will close `detached` state will come, and store the latest timer data in your shared preferences
if(state == AppLifecycleState.detached){
// store last timer data into shared preferences
}
print('Current state = $state');
}
@override
Widget build(BuildContext context) => Scaffold();
}
and when your application starts :
main.dart
or in your root widget.