Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix triggering onStopped and onEnded when restarting a stopped timer #37

Merged
merged 1 commit into from Sep 20, 2023

Conversation

aemelyanovff
Copy link
Contributor

Calling onResetTimer on a stopped timer triggers onStop and onEnded events. I expect these event not to trigger if the timer is not running.

Consider this use case. I want to create a countdown timer that restarts from the beginning every 5 seconds:

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  final StopWatchTimer _stopWatchTimer = StopWatchTimer(
    mode: StopWatchMode.countDown,
    presetMillisecond: 5000,
  );

  @override
  void initState() {
    super.initState();
    _stopWatchTimer.fetchEnded.listen((v) {
      print('Ended, value: $v');
      _stopWatchTimer.onResetTimer();
      _stopWatchTimer.onStartTimer();
    });
    _stopWatchTimer.onStartTimer();
  }

  @override
  void dispose() async {
    super.dispose();
    await _stopWatchTimer.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container();
  }
}

I expect this code to work, but every time in calls onResetTimer it also triggers fetchEnded, so it enters an infinite loop.

This fix replaces isRunning || _startTime > 0 with isRunning && _startTime > 0, so now resetting a stopped container doesn't trigger these events.

Copy link
Owner

@hukusuke1007 hukusuke1007 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

@hukusuke1007 hukusuke1007 merged commit 3652f8b into hukusuke1007:master Sep 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants