What does emit.forEach()
do? From what I understand, it takes a stream but how does the ui rebuild itself and what is the onData
parameter? I want to return a stream of data for a client matrix sdk
on<StartMatrixEventSubmitted>((event, emit) async {
emit(MatrixLoadingState());
await repo.getAllRooms().then((value) async {
await emit.forEach(
value.onSync.stream,
onData: (value2) {
print('data');
return MatrixSuccuClientState(rooms: value.rooms);
},
);
this.myRooms = value.rooms;
emit(MatrixClientSynced());
});
});
Here's the description of what emit.forEach
does in the docs:
Subscribes to the provided stream and invokes the onData callback when the stream emits new data and the result of onData is emitted.
Basically, it's the same as a List's .map()
method. Each and every data (element) in the list will be converted into another value. Eg: Object to String
emit.forEach
takes two values: the stream it has to listen to and the onData
function. It will listen to all of the values emitted by the stream and convert them into another value (In this case MatrixSuccuClientState
).
Think of value2
in onData: (value2)
as the "element[index]
in a list". The value2
is the currently emitted value of the stream. I think what you're looking for is:
onData: (currentlyEmittedValue) => MatrixSuccuClientState(rooms: currentlyEmittedValue.rooms),
I'd suggest understanding Streams
in flutter before moving on to bloc. I suggest watching this video. After that, you could learn how bloc works in this dev page