Flutter Inactivity Timeout

Viewed 109

I am trying to implement an inactivity timeout in my Flutter Web App. I have tried numberous methods so far. The best success I have had is by using the RestartableTimer class from the async library. However I am trying to see what other methods I can use.

Here is my relavant code snippet:

  late Timer logoutTimer;

  DateTime lastActivity = DateTime.now();

  AppModel() {
    logoutTimer = Timer.periodic(kInactivityTimeoutDev, (timer) {
      print(
          '$kInactivityTimeoutDev <= ${lastActivity.difference(DateTime.now())} == ${kInactivityTimeoutDev <= lastActivity.difference(DateTime.now())}');

      if (kInactivityTimeoutDev <= lastActivity.difference(DateTime.now())) {
        print('Signing out');
        FirebaseAuth.instance.signOut();
      }
    });
  }

  void resetInactivity() {
    lastActivity = DateTime.now();
  }

Basically whenever I do a certain action in the app (button clicks, API calls, etc.) I am resetting the lastActivity variable. This part seems to work, but my condition that I check for the timeout seems to always return false no matter what.

I have set

kInactivityTimeoutDev = Duration(seconds: 45);

And here is a sample of the output I get when testing this method:

0:00:45.000000 <= 0:00:41.156000 == false
0:00:45.000000 <= 0:01:26.170000 == false
0:00:45.000000 <= 0:02:11.169000 == false
0:00:45.000000 <= 0:02:56.163000 == false
0:00:45.000000 <= 0:03:41.164000 == false
0:00:45.000000 <= 0:04:26.165000 == false
0:00:45.000000 <= 0:05:11.155000 == false
0:00:45.000000 <= 0:05:56.170000 == false
0:00:45.000000 <= 0:06:41.157000 == false
0:00:45.000000 <= 0:07:26.161000 == false
0:00:45.000000 <= 0:08:11.164000 == false

I don't understand how this condition is always coming out to false. If anybody can see something that I am not seeing I would appreciate it.

0 Answers
Related