yield BLoC state inside a timer's recurring callback
Asked Answered
O

1

5

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);
        });
}
Olia answered 13/6, 2020 at 12:23 Comment(0)
O
6

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);
}
Olia answered 14/6, 2020 at 5:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.