hope you're all are doing well and #stayhome and keep #socialdistancing :)
I do have a small problem with the flutter_bloc library and I'm hoping that someone can help me out.
I have an API-Call (moviesStream
), which returns a stream.
What I would like to have is: on every yield
from that stream do a yield
in my bloc
and therefore rebuild my widget per item and not at the end for all items
. I want to build the items
successively one after the other
This is my approach and I don't get why it doesn't work. I am very open for any other approach to achieve my goal
What I've learned so far: I cannot yield the same state but my old and new one are different, so that should not be a problem (I think). Do I need to dispatch/add a new Event (or the same again) after yielding? I understand that maybe this would solve my problem, but that would start my stream again from the beginning, wouldn't it?
Stream<FilmeState> mapEventToState(FilmeEvent event) async* {
final currentState = state;
yield FilmeStateUninitialized();
if (event is FilmeEventLoad)
{
yield FilmeStateLoadedV2(items: []);
var moviesStream = Stream.fromIterable([{"1":"1"}, {"2":"2"}, {"3":"3"}]);
await for (var value in moviesStream) {
var newItems = List<Map<String, dynamic>>.from((state as FilmeStateLoadedV2).items);
newItems.add(value);
print("newItems.length=${newItems.length}");
yield FilmeStateLoadedV2(items: newItems);
}
}
The output is the following
I/flutter (12087): state=Instance of 'FilmeStateUninitialized' // this comes from my widget (BlocBuilder)
I/flutter (12087): newItems.length=1
I/flutter (12087): newItems.length=2
I/flutter (12087): newItems.length=3
I/flutter (12087): state=Instance of 'FilmeStateLoadedV2' // this comes from my widget (BlocBuilder)
I was expecting or rather I wish to have the following output
I/flutter (12087): state=Instance of 'FilmeStateUninitialized' // this comes from my widget (BlocBuilder)
I/flutter (12087): newItems.length=1
I/flutter (12087): state=Instance of 'FilmeStateLoadedV2' // this comes from my widget (BlocBuilder)
I/flutter (12087): newItems.length=2
I/flutter (12087): state=Instance of 'FilmeStateLoadedV2' // this comes from my widget (BlocBuilder)
I/flutter (12087): newItems.length=3
I/flutter (12087): state=Instance of 'FilmeStateLoadedV2' // this comes from my widget (BlocBuilder)