-1

I'm trying to use the Streambuilder in cooperation with Bloc. The problem is that somehow the UI updates only when the expensive Funktions are finished. It seems that then, and only then updates are made to the stream. But I can not figure out why?

I Implemented a simple Bloc that has 4 events: 1. a Future called over the Bloc Sate Object 2. a Future called inside the Bloc 3. a Funktion called inside the Bloc 4 just using Future.delay

I'm Trying to understand why everything behaves as expectetd, but not when I use first two. My guess is I have a basic misunderstanding of the eventloop but I can not see why there should be a difference in behavior between 2 and 4 or 1 and 4

To make it easy I made an example Project on Github https://github.com/pekretsc/bloceventloop.git

So I have my refresh methode that ads the new state to the Stream.

if (event is ExpensiveEventInState) {
  refresh(BlocUIState.Waiting);
  String result = await blocState.doSomeThing();
  if (result == '') {
    refresh(BlocUIState.Fin);
  } else {
    refresh(BlocUIState.Fail);
  }
}

    if (event is ExpensiveEventWhyDoesThisWork) {
  refresh(BlocUIState.Waiting);
  await Future.delayed(Duration(seconds: 3));
  refresh(BlocUIState.Fin);
}

so the question is, should the first and second event not behave the same way? What happens though is that in the first case the refresh is ignored completely and just the Fin is added to the stream. (I checked that, its not that it is too fast to recognize)

Stefan Zobel
  • 3,182
  • 7
  • 28
  • 38

1 Answers1

0

StreamListener's callback is called immediately when an event is pushed on the stream.

On the other hand, StreamBuilder's builder callback isn't. For most common use-cases, it is called at most once per frame.

Rémi Rousselet
  • 256,336
  • 79
  • 519
  • 432
  • Ok so if that is the problem, shouldn't the Streambuilder still react before the waittime is over ? It does rebuild on the Future delay. So why not on the Future doSomeThing? in there I do the same thing, why ignore the first Streamevent – Peter Kretschmer Sep 11 '19 at 08:40