Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to execute code before app exit flutter

I want to detect when a user quit my app and execute some code before but I don't know how to do this. I tried to use this package: https://pub.dev/packages/flutter_lifecycle_state but I have this error:

flutter/.pub-cache/hosted/pub.dartlang.org/flutter_lifecycle_state-1.0.0/lib/flutter_lifecycle_state.dart:80:30: Error: Getter not found: 'suspending'. case AppLifecycleState.suspending

If you have any solution for this problem or know another way to detect when a user quit my app it could be cool

like image 688
luc Avatar asked Feb 12 '20 08:02

luc


2 Answers

You can not do exactly what you want to do right now, anyway, the best approach right now is to check when the application it’s running in background/inactive using the AppLifecycleState from the SDK (basically does what your library is trying to do)

The library that you are using it’s outdated, since a pull request from November 2019 the AppLifecycleState.suspending it’s called AppLifecycleState.detached.

You can take a look at the AppLifecycleState enum in the api.flutter.dev website

Here’s an example of how to observe the lifecycle status of the containing activity:

import 'package:flutter/widgets.dart';  class LifecycleWatcher extends StatefulWidget {   @override   _LifecycleWatcherState createState() => _LifecycleWatcherState(); }  class _LifecycleWatcherState extends State<LifecycleWatcher> with WidgetsBindingObserver {   AppLifecycleState _lastLifecycleState;    @override   void initState() {     super.initState();     WidgetsBinding.instance.addObserver(this);   }    @override   void dispose() {     WidgetsBinding.instance.removeObserver(this);     super.dispose();   }    @override   void didChangeAppLifecycleState(AppLifecycleState state) {     setState(() {       _lastLifecycleState = state;     });   }    @override   Widget build(BuildContext context) {     if (_lastLifecycleState == null)       return Text('This widget has not observed any lifecycle changes.', textDirection: TextDirection.ltr);      return Text('The most recent lifecycle state this widget observed was: $_lastLifecycleState.',         textDirection: TextDirection.ltr);   } }  void main() {   runApp(Center(child: LifecycleWatcher())); } 

I think that deleting your data on the inactive cycle and then creating it again in the resumed one can work for you.

like image 94
Josep Bové Avatar answered Nov 11 '22 17:11

Josep Bové


The audio_service plugin does something very similar. The strategy is to wrap your app in a custom widget that listens for when the app life cycle state changes, and then run different code based on the state. I'm not saying you should use this plugin but that you can adapt the code to fit your needs. Replace references to AudioService below with whatever code you need to run.

Here is the code from audio_service:

/// A widget that maintains a connection to [AudioService]. /// /// Insert this widget at the top of your `/` route's widget tree to maintain /// the connection across all routes. e.g. /// /// ``` /// return MaterialApp( ///   home: AudioServiceWidget(MainScreen()), /// ); /// ``` /// /// Note that this widget will not work if it wraps around [MateriaApp] itself, /// you must place it in the widget tree within your route. class AudioServiceWidget extends StatefulWidget {   final Widget child;    AudioServiceWidget({@required this.child});    @override   _AudioServiceWidgetState createState() => _AudioServiceWidgetState(); }  class _AudioServiceWidgetState extends State<AudioServiceWidget>     with WidgetsBindingObserver {   @override   void initState() {     super.initState();     WidgetsBinding.instance.addObserver(this);     AudioService.connect();   }    @override   void dispose() {     AudioService.disconnect();     WidgetsBinding.instance.removeObserver(this);     super.dispose();   }    @override   void didChangeAppLifecycleState(AppLifecycleState state) {     switch (state) {       case AppLifecycleState.resumed:         AudioService.connect();         break;       case AppLifecycleState.paused:         AudioService.disconnect();         break;       default:         break;     }   }    @override   Future<bool> didPopRoute() async {     AudioService.disconnect();     return false;   }    @override   Widget build(BuildContext context) {     return widget.child;   } } 

Note:

  • Exiting the app for most users usually just means hiding it. The app is still alive in the background until the system kills it to save resources.
like image 40
Suragch Avatar answered Nov 11 '22 17:11

Suragch