如何在颤振 Web 应用程序中注销 onDestroy

时间:2021-06-11 21:59:25

标签: flutter datetime flutter-web logout destroy

当应用程序被销毁时,如何从应用程序中注销用户。我制作了一个 Flutter Web 应用程序,其中我很容易成功注销用户并通过基本注销按钮记录用户注销的日期和时间,但我想记录用户点击关闭按钮或关闭的日期和时间应用程序无需注销。我该怎么做,任何人都可以帮忙

1 个答案:

答案 0 :(得分:0)

我认为State.dispose生命周期方法应该在这个状态销毁时调用,就像android Activity.onDestroy一样,无论是退出这个State(Activity)还是退出应用程序。状态处理时我必须做一些事情,但是当应用程序退出时没有调用处理方法。

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'),
    ),
  ),
);
 }
 }