【问题标题】:How to perform a task when user closed app flutter用户关闭应用程序颤动时如何执行任务
【发布时间】:2021-06-10 07:26:49
【问题描述】:

我想检测用户何时退出我的应用程序并执行一些代码,但我不知道该怎么做。我尝试了很多包但没有得到任何解决方案我只想在应用关闭时执行 Firestore 功能

【问题讨论】:

    标签: android flutter flutter-dependencies


    【解决方案1】:

    在你的情况下,你应该使用 Widgetbinding 观察者这里是示例代码:

        class Demo extends State with WidgetsBindingObserver{
    
        AppLifecycleState state;
    
        @override
        void initState() {
         // TODO: implement initState
         super.initState();
    
         WidgetsBinding.instance.addObserver(this);
         }
    
         @override
         void dispose() {
         // TODO: implement dispose
         super.dispose();
         WidgetsBinding.instance.removeObserver(this);
         }
    
    
         @override
         void didChangeAppLifecycleState(AppLifecycleState appLifecycleState) {
         // TODO: implement didChangeAppLifecycleState
         super.didChangeAppLifecycleState(state);
         state = appLifecycleState;
         /// STATES : active , inactive , paused , unknown , detached
        if(state == 'active'){
            /// your expression
           }
          }
    
    
          @override
          Widget build(BuildContext context) {
          return Scaffold(
    
          body:Text("Obersever"));
    
    
          }}
    
    

    这对你有用.. :)

    【讨论】:

      猜你喜欢
      • 2021-02-25
      • 1970-01-01
      • 2021-07-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多