flame icon indicating copy to clipboard operation
flame copied to clipboard

Timer with tickCount stays finished after start() or reset()

Open matthewalberto opened this issue 2 weeks ago • 1 comments

What happened?

A Timer (or TimerComponent) with tickCount never resets its tick counter. start(), stop() and reset() only set current back to 0, so once the timer has ticked tickCount times:

  • finished is still true right after start(), even though the timer is running again. A TimerComponent with removeOnFinish: true is removed on its next update.
  • The restarted timer ticks once and stops, instead of ticking tickCount times again.

reset() part way through has the same problem: the ticks counted before the reset still count towards the limit.

What do you expect?

start() starts the timer from 0, as its docs say, so it should tick tickCount times again and not be finished until it has. reset() should also reset the ticks.

How can we reproduce this?

var count = 0;
final timer = Timer(
  period: 1,
  repeat: true,
  tickCount: 2,
  onTick: () => count++,
);
timer.update(1.1);
timer.update(1.1);
print(count); // 2
print(timer.finished); // true

timer.start();
print(timer.finished); // true, expected false
timer.update(1.1);
timer.update(1.1);
print(count); // 3, expected 4

What steps should take to fix this?

Reset _currentTick in Timer.reset() (which start() and stop() call). When the tick limit is reached, _callTicker calls stop() today; it would need to set _current to 0 and pause instead, so that the timer still reports finished afterwards.

Affected platforms

All

Other information

Flame main at a0044089 (flame 2.0.0-dev.1), Flutter 3.47.9 stable. tickCount was added in #3552.

Are you interested in working on a PR for this?

  • [x] I want to work on this

matthewalberto avatar Nov 20 '26 02:11 matthewalberto

Fix in #4241

matthewalberto avatar Nov 20 '26 02:11 matthewalberto