'How to trigger dispose from another page

I have two pages, Page_1 which uses didChangeAppLifecycleState which looks out for when the user closes the app. While the user is on the app, a timer increases, as soon as the user closes the app, the timer stops.

Page_2 has a logout button, which uses firebase signOut() function.

If I were to use the logout function in Page_1 and sign in with another account, the timer would start new for the second user, but If I were to logout from my Page_2 and sign in with another user, then the timer would of carried from the first user instead of starting again,

What I'm asking is how can I use WidgetsBinding.instance!.removeObserver(this); on Page_2 instead of Page_1

Page_1.dart

class _HomeScreenState extends State<HomeScreen> with WidgetsBindingObserver {
  User? user = FirebaseAuth.instance.currentUser;
  UserModel loggedInUser = UserModel();
  final _auth = FirebaseAuth.instance;
  StreamController<int> controller = StreamController();
  late StreamSubscription<int> streamSubscription;

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance!.addObserver(this);
    print(user!.uid);
    FirebaseFirestore.instance
        .collection("users")
        .doc(user!.uid)
        .get()
        .then((value) {
      loggedInUser = UserModel.fromMap(value.data());
      setState(() {});
    });
    app_start = DateTime.now();
  }

  late DateTime app_start;
  late DateTime app_end;

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    print(state);
    // if user clicks logout end this funciton
    // Checks if app is active
    final isBackground = state == AppLifecycleState.paused;

    if (state == AppLifecycleState.inactive ||
        state == AppLifecycleState.detached) return;

    if (isBackground) {
      FirebaseFirestore.instance
          .collection("users")
          .doc(user!.uid)
          .get()
          .then((value) {
        loggedInUser = UserModel.fromMap(value.data());

        if (loggedInUser.timeActive == null) {
          loggedInUser.timeActive = 1;
         
        } else {
        
        }

        app_end = DateTime.now();
        final differenceInDays = app_end.difference(app_start).inSeconds;

        int? test = loggedInUser.timeActive;

        int? totalOnTime = differenceInDays + loggedInUser.timeActive!.toInt();
        FirebaseFirestore.instance
            .collection('users')
            .doc(loggedInUser.uid)
            .update({"timeActive": totalOnTime});
        setState(() {});
      });
    } else {
      app_start = DateTime.now();
    }
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    WidgetsBinding.instance!.removeObserver(this);
  }
}

Page_2.dart

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    WidgetsBinding.instance!.removeObserver(this);
  }

ElevatedButton(
onPressed: () {
  dispose()
_auth.signOut();
Navigator.of(context).pushReplacement(
MaterialPageRoute(
builder: (context) =>
LoginScreen()));
print("clicked");
},
child: Text('Log out'),
 )



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source