flutterdarttimerperiodic-task

Accessing and cancelling a periodic timer in dart/flutter


I am using a periodic timer for fetching data from server. I need to conditionally stop the timer when a bool (userExpectingTimerBool) has been set to false and also explicitly stop it by user. After stop, neither the periodic timer _timer, nor the last scheduled callback timer _t (or t) should run.

I have a fully working code now. But I feel some of it is redundant and some aspects of periodic timer is still not clear to me after reading documentation. Code is included below.


I have four questions:

  1. Do I need to stop both _timer and _t (or t) to stop all scheduled tasks?
  2. Which timer should I use to display search count (t.tick/_t.tick/_timer.tick)?
  3. The callback function is a transient (nameless) method. Is the timer passed as an argument to it (t) a persistent or transient object (i.e., its value will be preserved across runs)?
  4. What parts of the below code are redundant/can be reduced?

periodic timer with conditional run:

Timer _timer;
Timer _t;

bool userExpectingTimerBool=false;
var timerSearchCount;
var tSearchCount;

_timer = Timer.periodic(
   Duration(seconds: intervalGiven), 
   (Timer t) { 
        _t= t; 
        if (userExpectingTimerBool == false) t.cancel(); 
        tSearchCount=t.tick; 
        _displayResult(); 
   }
); 

timerSearchCount=_timer?.tick;

user-controlled stop timer function:

  void stoptimer(){
    setuserExpectingTimerBool(false);
     if (_t !=null){_t!.cancel();}
     if (_timer !=null){_timer!.cancel();}
     checktimerActive();
  }

Solution

    1. When Timer.periodic invokes its callback, it should invoke it with itself as the callback's Timer argument. Therefore _t is just an alias for _timer, and having both is pointless. (You can verify this for yourself by printing identityHashCode(timer) and comparing against identityHashCode(_timer).)

    2. See #1. Does not matter.

    3. See #1. There is a single Timer object created by Timer.periodic.

    4. if (userExpectingTimerBool == false)
      

      If userExpectingTimerBool is not nullable, then it would be clearer as if (!userExpectingTimerBool). (If you believe that == false is more readable, then why not if ((userExpectingTimerBool == false) == true) and if (((userExpectingTimerBool == false) == true) == true), etc.) userExpectingTimerBool seems unnecessary anyway; if stoptimer is called, the Timer will be cancelled and prevent additional callbacks. You therefore shouldn't need to perform an additional cancellation check within the callback.

      if (_timer !=null){_timer!.cancel();}
      

      As written, _timer is non-nullable, so the null check is unnecessary. If you intend for _timer to be nullable, then you can replace the explicit if check with just _timer?.cancel();.