Is there anyway to detect app closed on flutter?

3,304

Sorry, there isn't. You can only detect being put in the background not being force-closed.

Share:
3,304
Purinut
Author by

Purinut

Updated on December 21, 2022

Comments

  • Purinut
    Purinut over 1 year

    I trying to detect the app close on flutter. Is there any way possible on dart?

    I try using WidgetsBindingObserver but flutter can only detect AppLifecycleState of paused, inactive (I believe that on IOS), resumed, and detached.

    class ChatScreenState extends State<ChatScreen> with WidgetsBindingObserver{
      @override
      void didChangeAppLifecycleState(AppLifecycleState state) {
        super.didChangeAppLifecycleState(state);
    
        setState(() {
          _notification = state;
        });
    
        switch (state) {
          case AppLifecycleState.paused:
            print('paused');
            break;
          case AppLifecycleState.inactive:
            print('inactive');
            break;
          case AppLifecycleState.resumed:
            print('resumed');
            break;
          case AppLifecycleState.detached:
            print('detached');
            break;
        }
      }
    }
    

    which I try closing my app its print only paused.

    What I'm trying to do is when the app closed on chatscreen. I want to write something on my firestore. But I can't find a way to do this.

    Edit: what I mean by closed that is I intentionally close the app myself. (press home button and swipe up)

    this is terminal log when the app closed

    D/EGL_emulation( 9248): eglMakeCurrent: 0xdb81aba0: ver 3 0 (tinfo 0xdb80fa70)
    I/flutter ( 9248): state = AppLifecycleState.paused <- after I try send app to background 
    I/flutter ( 9248): state = AppLifecycleState.inactive
    I/flutter ( 9248): state = AppLifecycleState.resumed
    D/EGL_emulation( 9248): eglCreateContext: 0xe39acc80: maj 3 min 0 rcv 3
    D/EGL_emulation( 9248): eglMakeCurrent: 0xe39acc80: ver 3 0 (tinfo 0xd840fd90)
    D/EGL_emulation( 9248): eglMakeCurrent: 0xdb81aba0: ver 3 0 (tinfo 0xdb80fa70)
    D/EGL_emulation( 9248): eglMakeCurrent: 0xe39acc80: ver 3 0 (tinfo 0xd840fd90)
    I/flutter ( 9248): state = AppLifecycleState.inactive
    D/EGL_emulation( 9248): eglMakeCurrent: 0xdb81aba0: ver 3 0 (tinfo 0xdb80fa70)
    I/flutter ( 9248): state = AppLifecycleState.paused <- after I close my app
    Lost connection to device.
    

    P.S. I new to StackOverflow, and flutter