skip to Main Content

didChangeAppLifecycleState is not working. I have checked all other question related to this, but not helpful.

    @override
    void initState() {
      WidgetsBinding.instance.addObserver(this);
      super.initState();
    }

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

    @override
    void didChangeAppLifecycleState(AppLifecycleState state) {
      // TODO: implement didChangeAppLifecycleState
      UpPrint(name: "CheckUserStatus", value: "Before");
      print(state);
      UpPrint(name: "CheckUserStatus", value: "After");
      if (state == AppLifecycleState.resumed) {
        //TODO: set status to online here in firestore
        UpPrint(name: "CheckUserStatus", value: "Online");
        UpFirebaseDB.updateUserStatus(status: true);
      } else {
        //TODO: set status to offline here in firestore
        UpFirebaseDB.updateUserStatus(status: false);
        UpPrint(name: "CheckUserStatus", value: "Offline");
      }
    }

Note: I have used the initialRoute: ‘/’, onGenerateRoute: UpRoutes.onGenerateRoute, for page navigation and i’m using the Getx for state management.

Thank in Advanced.

2

Answers


  1. Make sure that you have initialized WidgetsFlutterBinding in your main() function, ensuring that your Flutter app is properly set up to handle lifecycle events.

    ex:

    void main() {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(MyApp());
    }
    
    Login or Signup to reply.
  2. Unsure about the setup of your application but I can provide you with the base code that works on my end.

    First, create a lifecylce_manager.dart file which includes:

    class LifeCycleManager extends StatefulWidget {
    final Widget child;
    const LifeCycleManager({super.key, required this.child});
    
    @override
    LifeCycleManagerState createState() => LifeCycleManagerState();
    }
    
    class LifeCycleManagerState extends State<LifeCycleManager>
      with WidgetsBindingObserver {
    
    @override
    void initState() {
     WidgetsBinding.instance.addObserver(this);
     super.initState();
    }
    
    @override
    void dispose() {
      WidgetsBinding.instance.removeObserver(this);
      super.dispose();
    }
    
    @override
    void didChangeAppLifecycleState(AppLifecycleState state) {
      // TODO: implement didChangeAppLifecycleState
      UpPrint(name: "CheckUserStatus", value: "Before");
      print(state);
      UpPrint(name: "CheckUserStatus", value: "After");
      if (state == AppLifecycleState.resumed) {
        //TODO: set status to online here in firestore
        UpPrint(name: "CheckUserStatus", value: "Online");
        UpFirebaseDB.updateUserStatus(status: true);
      } else {
        //TODO: set status to offline here in firestore
        UpFirebaseDB.updateUserStatus(status: false);
        UpPrint(name: "CheckUserStatus", value: "Offline");
      }
    }
    @override
    Widget build(BuildContext context) {
      return Container(
        child: widget.child,
      );
     }
    }
    

    Then, in the main.dart file or your applications entry point:

    void main() async {
      WidgetsFlutterBinding.ensureInitialized();
      runApp(const MyApp());
    }
    
    class MyApp extends StatelessWidget {
    const MyApp({super.key});
    
    @override
    Widget build(BuildContext context) {
      return LifeCycleManager(
        child: MaterialApp(
          initialRoute: '/',
          onGenerateRoute: UpRoutes.onGenerateRoute,
          theme: appTheme(),
        ),
      );
     }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search