Hi,
I have a question, is it possible to yield a new State not from the mapEventToState pipeline?
I will explain,
Consider a bloc that has an internal subscription to a stream from some dependency of it.
The subscription is established in the bloc constructor.
When that stream emit event, I want to emit new Bloc State
Something like this:
`
class MyBloc extends Bloc<BlocEvent, BlocState> {
final AuthManager authManager;
MyBloc({this.authManager}){
_initializeAuthStream();
}
Stream<BlocState> mapEventToState(BlocEvent event){
if(event is UserStateEvent){
yield* _handleUserStateChanged(event); //This eventually will emit new State
}
}
void _initializeAuthStream() {
authManager.userStateStream.listen((userState) => {
//How can I yield a state from here?
//Currently I do like this:
add(UserStateEvent(state: userState)) //This will add new event to the bloc
})
}
}`
Thanks for your support
Hi @MotiBartov 馃憢
You can't directly yield a state from your onData callback, e.g: you can't do something like:
yield* authManager.userStateStream.listen((userState) async* { yield Authenticated(); }) since listen returns a StreamSubscription not a Stream.
Bloc's state should only be updated by yielding a new state from mapEventToState in response to an event. So your current approach is the correct one. 馃憤
Thanks @RollyPeres for taking the time to respond!
@MotiBartov closing this for now but feel free to comment with additional questions and I鈥檓 happy to continue the conversation 馃憤
Most helpful comment
Hi @MotiBartov 馃憢
You can't directly yield a state from your
onDatacallback, e.g: you can't do something like:yield* authManager.userStateStream.listen((userState) async* { yield Authenticated(); })sincelistenreturns aStreamSubscriptionnot aStream.Bloc's state should only be updated by yielding a new state from
mapEventToStatein response to an event. So your current approach is the correct one. 馃憤