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
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:
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:
Then, in the main.dart file or your applications entry point: