skip to Main Content

*** UPDATE ***

I have a problem. When I use a Timer in Flutter and cancel() it afterwards, but as soon the cancel method is triggered, nothing happens.

var counter = 0;

Timer.periodic(const Duration(milliseconds: 50), (timer) {

developer.log('Actual counter ' + counter.toString());
counter++;
 
if (counter == 5) {
   developer.log('TIMER DONE');
    timer.cancel();
}

});

developer.log('This comes afterwards');

Expected out put:

Actual counter 0
Actual counter 1
Actual counter 2
Actual counter 3
Actual counter 4
TIMER DONE
This comes afterwards

The Output:

Actual counter 0
Actual counter 1
Actual counter 2
Actual counter 3
Actual counter 4
TIMER DONE

normally I should see a message ‘this is afterwards’ in the console, but it seems that with the cancel the complete method will be canceled.

Many thanks in advance.

2

Answers


  1. Update: perform operation end of timer.

    You need to add logic inside if condition to perform operation.

    if (counter == firmwareList.length) {
        timer.cancel();
        developer.log('This comes afterwards'); //here
          }
    });
    
    
    

    Here is an widget example

    class TimerF extends StatefulWidget {
      const TimerF({super.key});
    
      @override
      State<TimerF> createState() => _TimerFState();
    }
    
    class _TimerFState extends State<TimerF> {
      var counter = 0;
    
      Timer? timer;
    
      bool isTimerActive = true;
    
      @override
      void initState() {
        super.initState();
        timer = Timer.periodic(const Duration(seconds: 1), (timer) {
          print('Still timer');
    
          if (counter == 5) {
            timer.cancel();
            isTimerActive = false;
          }
          counter++;
          setState(() {});
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: Column(
            children: [
              Text("current value ${counter}"),
              if (timer?.isActive == false) Text("Timer isnt active"),
              if (isTimerActive == false) Text("Timer isnt active $isTimerActive"),
            ],
          ),
        );
      }
    }
    

    I can’t see any increment on counter on current snippet. that’s why counter remains 0 and doesnt meet the condition to cancel the timer.

    Perhaps it will be

    var counter = 0;
    Timer.periodic(const Duration(milliseconds: 50), (timer) {
      print('Still timer');
    
      if (counter == 5) {
        timer.cancel();
      }
      counter++; //increment the value
      print('this is afterwards');
    });
    
    Login or Signup to reply.
  2. Here is a fully working example using a Timer

    Timer? timer;
    
    startTimer() {
      var counter = 0;
      timer = Timer.periodic(const Duration(milliseconds: 50), (timer) {
        counter += 1;
        print('Actual step $counter');
        if(counter==5) {
          cancelTimer();
        }
      });
    }
    
    cancelTimer() {
      timer?.cancel();
      print('this is afterwards');
      //do something here
    }
    

    When you startTimer(), here is the output

    Actual step 1
    Actual step 2
    Actual step 3
    Actual step 4
    Actual step 5
    this is afterwards
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search