Dart/Flutter Yield state after timer

Issue

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.

Solution

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

mapEventToState

Answered By – Prabhanshu Tiwari

Answer Checked By – Marilyn (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *