How to log out onDestroy in flutter web app

Viewed 453

How can I logout the user from the application when it is destroyed. I have made an flutter web application in which it is easy for me to log out the user successfully and note the date and time of user logout through basic logout button but I want to note the date and time when user hit the close button or close the application without logging out. How can I do that can anyone help

1 Answers

i think State.dispose lifecycle method should called when this state destroy,just like android Activity.onDestroy, whether quit this State(Activity) or quit app. I have to do something when state dispose, but when app quit dispose method not called.

import 'package:flutter/material.dart';
 import 'package:flutter/services.dart';

 void main() => runApp(MyApp());

   class MyApp extends StatelessWidget {
 // This widget is the root of your application.
@override
  Widget build(BuildContext context) {
    return MaterialApp(
  title: 'Flutter Demo',
  theme: ThemeData(primarySwatch: Colors.blue, primaryColor: Colors.white),
  home: MainActivity(),
);
 }
}

class MainActivity extends StatefulWidget {
  @override
  _MainActivityState createState() => _MainActivityState();
}

 class _MainActivityState extends State<MainActivity>
with WidgetsBindingObserver {
 @override
 void initState() {
super.initState();

print('initState');
WidgetsBinding.instance.addObserver(this);
}

@override
 void deactivate() {
  super.deactivate();
//this method not called when user press android back button or quit
print('deactivate');
}

@override
void dispose() {
super.dispose();
WidgetsBinding.instance.removeObserver(this);

//this method not called when user press android back button or quit
print('dispose');
}

@override
 void didChangeAppLifecycleState(AppLifecycleState state) {
 //print inactive and paused when quit
  print(state);
 }

 @override
 Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Title'),
  ),
  body: Center(
    child: RaisedButton(
      onPressed: () {
        SystemNavigator.pop();
      },
      child: Text('Quit'),
    ),
  ),
);
 }
 }
Related