Is there anyway to detect app closed on flutter?
Asked Answered
K

1

7

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

Katharinekatharsis answered 28/5, 2020 at 7:37 Comment(7)
What do you mean by "closed"?Nonjuror
I intentionally close the app myself. @NonjurorKatharinekatharsis
you can use something called Willpopscope to detect when the user clicks the back buttonGaff
I already did that, What I mean by closed I swipe to close the app like press home button and swipe up. @UniKatharinekatharsis
Try asking the official flutter discord group that but as far as I know its not possible. You can ask the people in the discord group. They are more knowledgeable than me.Gaff
it's not possible you mean that I have to do in the native code right? btw I did ask a question a flutter discord server thanks a lot mate. @UniKatharinekatharsis
Did find a solution to this. I want to implement and online/offline feature in my app. So i need know if the app about to close and call the function to write on firebase.Catie
B
5

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

Bateman answered 28/5, 2020 at 8:24 Comment(1)
No, iOS you just can't do it as far as I know.Bateman

© 2022 - 2024 — McMap. All rights reserved.