yield BLoC state inside a timer's recurring callback

Viewed 878

How can I yield a state from the recurring callback of a Timer?

I have a Timer object in my class and when user hits start, a new object of it is created, and inside it's callback I need to call yield ....
This is the code I have so far, but it's not doing anything:

if (event is CounterETimerStart) {
        timer = Timer.periodic(Duration(seconds: 1), (timer) async* {
          yield CounterNewSecond(++m.passedTime);
        });
}
1 Answers

With help from @RollyPeres from github, this is one approach:

You can add an event and react to it.

if (event is CounterETimerStart) {
        timer = Timer.periodic(Duration(seconds: 1), (timer) {
              add(TimerTicked());
          });
    }
if (event is TimerTicked) {
   yield CounterNewSecond(++m.passedTime);
}
Related