skip to Main Content

i am trying to make it so that when the timer finishes, it automatically restarts but it seems i am running into some problems. here is the code

int _start = 7;
  int? selectedValue;


  void startTimer() {
    const oneSec = Duration(seconds: 1);
    _timer = Timer.periodic(
      oneSec,
          (Timer timer) {
        if (_start == 0) {
          setState(() {});
        } else {
          setState(() {
            _start--;
          });
        }
      },
    );
  }
  void restartTimer() {
    if (_timer != null) {
      _timer.cancel();
    }
   setState(() {
     _start = 5;
   });
    startTimer();
  }
  @override
  void initState() {
    // TODO: implement initState
    startTimer();
    restartTimer();

i would appreciate a fix to this problem

3

Answers


  1. you need to call restartTimer() after the timer reaches zero

    if (_start == 0) {
        setState(() {});
        restartTimer();
    } 
    

    I would also suggest allowing zero and restarting after

    if (_start < 0) {
        restartTimer();
    } 
    
    Login or Signup to reply.
  2. If you could send the whole snippet it may be easier to see what is wrong but from what I can already see, you put the restart timer method in the initState, which means, the method will be called only once and at the wrong time.

    What I would suggest is:

         int _start = 7;
          int? selectedValue;
        
        
          void startTimer() {
            const oneSec = Duration(seconds: 1);
            _timer = Timer.periodic(
              oneSec,
                  (Timer timer) {
                if (_start == 0) {
                  setState(() {
                    restartTimer();
                  });
                } else {
                  setState(() {
                    _start--;
                  });
                }
              },
            );
          }
          void restartTimer() {
            if (_timer != null) {
              _timer.cancel();
            }
           setState(() {
         _start = 5;
       });
        startTimer();
      }
      @override
      void initState() {
        // TODO: implement initState
        startTimer();
        
    

    Here I took away the restart timer method and put it inside your start method where you check if the countdown is 0 or not.
    Don’t forget to not put the int _start = 7 into the build, else when you will set the state again, the "start" will be 7 instead of 5 as you intended in your method (and the _start– won’t work either).

    Hope it helped.

    Login or Signup to reply.
  3. Change this way,

    void startTimer() {
          const oneSec = Duration(seconds: 1);
          _timer = Timer.periodic(
            oneSec,
            (Timer timer) {
              if (_start == 0) {
                setState(() {});
                restartTimer();
              } else {
                setState(() {
                  _start--;
                });
              }
            },
          );
        }
    

    and

      @override
        void initState() {
          super.initState();
          startTimer();
        }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search