6

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

Purinut
  • 213
  • 4
  • 9
  • What do you mean by "closed"? – saturov May 28 '20 at 07:40
  • I intentionally close the app myself. @saturov – Purinut May 28 '20 at 07:41
  • you can use something called Willpopscope to detect when the user clicks the back button – CoderUni May 28 '20 at 07:51
  • I already did that, What I mean by closed I swipe to close the app like press home button and swipe up. @Uni – Purinut May 28 '20 at 08:04
  • 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. – CoderUni May 28 '20 at 08:09
  • 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. @Uni – Purinut May 28 '20 at 08:27
  • 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. – b.john Jun 13 '21 at 04:24

1 Answers1

5

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

Agreensh
  • 1,305
  • 1
  • 12
  • 15