简体   繁体   中英

Dart/Flutter Yield state after timer

I am using flutter_bloc and I am not sure how to yield state from within a callback. Am trying to start timer and return a new state after a pause. Reason I am using a timer is to get the ability to cancel previous timer so it always returns a new state after an idle state.

@override
  Stream<VerseState> mapEventToState(
    VerseEvent event,
  ) async* {
if (event is ABCEvent) {
Timer(const Duration(seconds: 3), () {
        print("Here");
        _onTimerEnd(); // adding yield here returns an error.
      })
}

Stream<XYZState> _onTimerEnd() async* {
    print("Yielding a state");
    yield myNewState();
  }

I can see that the code is getting inside the timer callback as I can see the print statements in the callback but not in the timerEnd() method.

State should be yielded by the stream used in bloc that you are current working on. Like

mapEventToState

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM