How do I logout time-based in Flutter App?
03:23 19 May 2021

I need to logout user from app if user has no reaction to app or inactive more than 5 minutes after login. the app will go to main login page.

I tried to implement the given solution here but not got success. Please help me how to achieve this.

My Code

    class AppRootState extends State {
    
          Timer _rootTimer;
        
          @override
          void initState() {
            // TODO: implement initState
            super.initState();
            initializeTimer();
          }
        
          void initializeTimer() {
           
            const time = const Duration(minutes: 5);
            _rootTimer = Timer(time, () {
              logOutUser();
            });
          }
        
          void logOutUser() async {
           
             // Log out the user if they're logged in, then cancel the timer.
            // You'll have to make sure to cancel the timer if the user manually logs out
            //   and to call _initializeTimer once the user logs in
            
            _rootTimer?.cancel();
    
          }
        
          // You'll probably want to wrap this function in a debounce
        
        void _handleUserInteraction([_]) {
            if (_rootTimer != null && !_rootTimer.isActive) {
              // This means the user has been logged out
              return;
            }
        
            _rootTimer?.cancel();
            initializeTimer();
          }
        
          @override
      Widget build(BuildContext context) {
        return Listener(
          behavior: HitTestBehavior.translucent,
          onPointerDown: _handleUserInteraction,
          onPointerMove: _handleUserInteraction,
          onPointerUp: _handleUserInteraction,
          child: MaterialApp(
    )
    );
    }
    
        }

I tried with Listener and GestureDetector but it's not working. User logged out even actively using the app.

flutter dart timer