在计时器的循环回调中产生 BLoC 状态
yield BLoC state inside a timer's recurring callback
如何从 Timer
的循环回调中 yield
一个状态?
我的 class 中有一个 Timer 对象,当用户点击开始时,它会创建一个新对象,在它的回调中我需要调用 yield ...
.
这是我目前的代码,但它什么也没做:
if (event is CounterETimerStart) {
timer = Timer.periodic(Duration(seconds: 1), (timer) async* {
yield CounterNewSecond(++m.passedTime);
});
}
在来自 github 的@RollyPeres 的帮助下,这是一种方法:
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);
}
如何从 Timer
的循环回调中 yield
一个状态?
我的 class 中有一个 Timer 对象,当用户点击开始时,它会创建一个新对象,在它的回调中我需要调用 yield ...
.
这是我目前的代码,但它什么也没做:
if (event is CounterETimerStart) {
timer = Timer.periodic(Duration(seconds: 1), (timer) async* {
yield CounterNewSecond(++m.passedTime);
});
}
在来自 github 的@RollyPeres 的帮助下,这是一种方法:
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);
}