flutterblocstate-managementcubit

BlocBuilder vs BlocListener


I have a very specific question when reading the documentation.

After reading the BlocBuilder documentation, I then went on by reading about the BlocListener. Everything was pretty clear, until I read this:

The listener is guaranteed to only be called once for each state change unlike the builder in BlocBuilder.

As I understand, in BlocBuilder the builder: gets called every time the bloc state changes (of course in the case in which buildWhen: is omitted and it always returns true as a default value). And in BlocListener the listener: gets called in response to state changes in the bloc.

I can't seem to understand the concept behind the quoted text. Doesn't the builder: in BlocBuilder also get called once for every change in the state of the bloc? Where's that "second" call I'm missing happening?


Solution

  • The builder is run as you say upon state change. But the builder function is also run when the framework deems necessary to rebuild.

    The listener function is not affected by the frameworks need to rebuild.