0

*** 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.

7
  • Can you include full sample widget how you are using this snippet Commented Jan 17, 2023 at 16:01
  • No not really, because this is everything I want to know. If you put the timer into a method and cancel it after the condition is done, it seems the timer cancel stops everything afterwards. Commented Jan 17, 2023 at 16:20
  • you like to when the timer is canceled ? Commented Jan 17, 2023 at 16:21
  • Like in my example. AFTER I canceled the timer (which was successful), I want to execute another code in the same method. This is not working. Like in my example I don't receive the log 'this comes afterwards'. Commented Jan 17, 2023 at 16:23
  • added complete workflow on update Commented Jan 17, 2023 at 16:28

2 Answers 2

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 doesn't 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');
});
Sign up to request clarification or add additional context in comments.

2 Comments

Oh yes sorry, I changed the code.
Can you include full sample widget, find more about minimal-reproducible-example
0

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

3 Comments

OK, can you please give me an example what happens if you want to execute something else after timer?.cancel()?
I edited my answer. You can add instructions just after canceling the timer. Regardless if I print before or after canceling the timer, the output is still the same.
Many Thanks, but it's not 100% solving my problem. As you can see I changed my code again (above) to visualize the problem I faced. I want to cancel the timer and afterwards (after the timer) I want to execute some code. I may have a logic problem here. I think I have to exclude the code here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.