Bloc: [Question] Why doesn't BLoC work with StreamBuilder?

Created on 24 Oct 2020  路  3Comments  路  Source: felangel/bloc

I'm on bloc 6.0.3 and flutter_bloc 6.0.6.

I tried to use StreamBuilder with a BLoC instance. The stream looks like this (with RxDart):

final blocStream = bloc.startWith(bloc.state);

and usage:

return StreamBuilder<BlocState>(
  stream: blocStream,
  builder: (context, snapshot) {
  ...
  },
);

(The code is actually in different pages, but this shouldn't be relevant here.)

I was expecting to be able to get BlocState instances in the snapshot, but nothing happens.

When I debug (or simply print whenever anything changes in the BLoC), I can actually see that the states of the BLoC change as expected, but they are never passed to my StreamBuilder. When I enter the screen with the builder again, it works because now the state (bloc.state) is already a success so the bloc.startWith(blos.state) line actually starts the stream with a success state.

Why was my expectation flawed, why isn't a BLoC a 'stream of states', a stream that would work with StreamBuilder?

It all works fine when I change it to BlocBuilder, so this is the way to go, I guess. But I would like to understand why it doesn't work, for the future, because apparently a BLoC is not a simple Stream of states as I used to think. I would also prefer to use a Stream, not a BLoC, whenever I can, but it's Ok if I can't.

question

Most helpful comment

Hi @felangel , thanks for the response. No, missing initialData isn't the reason, it's our app, again. While I was trying to make the simplest possible example to show our issue, I couldn't reproduce it, it all works fine (with or without initialData), which means our app is somehow broken. I need to rethink my bug issuing strategy, sorry for the noise, again.

All 3 comments

Hi @wujek-srujek 馃憢
Thanks for opening an issue!

I believe the issue is you鈥檙e not providing an initialData in the case of the StreamBuilder.

return StreamBuilder<MyState>(
  initialData: bloc.state,
  stream: bloc,
  builder: (context, snapshot) {
  ...
  },
);

Let me know if that helps 馃憤

Hi @felangel , thanks for the response. No, missing initialData isn't the reason, it's our app, again. While I was trying to make the simplest possible example to show our issue, I couldn't reproduce it, it all works fine (with or without initialData), which means our app is somehow broken. I need to rethink my bug issuing strategy, sorry for the noise, again.

@wujek-srujek no worries! Let me know if there's anything I can do to help 馃憤

Was this page helpful?
0 / 5 - 0 ratings

Related issues

RobPFarley picture RobPFarley  路  3Comments

clicksocial picture clicksocial  路  3Comments

nerder picture nerder  路  3Comments

shawnchan2014 picture shawnchan2014  路  3Comments

craiglabenz picture craiglabenz  路  3Comments