Bloc: Rebuilding the widget when showing error message in snackbar

Created on 14 Apr 2019  ยท  6Comments  ยท  Source: felangel/bloc

Hi,

I have a listview in the bloc builder. Before returning the list in the builder I am checking for error and showing a snackbar:

   if (state is GenericError) {
                _onWidgetDidBuild(() {
                  Scaffold.of(context).showSnackBar(
                    SnackBar(
                      content: Text('${state.error}'),
                      backgroundColor: Colors.red,
                    ),
                  );
                });
              }

The issue is that when there is an error the whole widget is built again and the list ends up being empty.

Also if my last state was an error state and when the widget is built again (eg. keyboard close or open) the snackbar is shown everytime.

question

Most helpful comment

Wow, I was just thinking if a feature something like that could be added
and it was already there. Great this is cool๐Ÿ‘

On Mon, Apr 15, 2019, 12:12 AM Felix Angelov <[email protected]
wrote:

Closed #217 https://github.com/felangel/bloc/issues/217.

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/217#event-2275038146, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AQ2HyeCBgHnF_ge1RtHABENi84XDhxNpks5vg3Z5gaJpZM4cuxSP
.

All 6 comments

Hi @Sourabhlpu ๐Ÿ‘‹
Thanks for opening an issue!

We recently added BlocListener to flutter_bloc v0.10.0. BlocBuilder should not be used for one-time actions like showing a SnackBar because the builder method can be called multiple times by Flutter.

Check out the SnackBar Recipe for an example ๐Ÿ‘

Hope that helps!

Wow, I was just thinking if a feature something like that could be added
and it was already there. Great this is cool๐Ÿ‘

On Mon, Apr 15, 2019, 12:12 AM Felix Angelov <[email protected]
wrote:

Closed #217 https://github.com/felangel/bloc/issues/217.

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/217#event-2275038146, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AQ2HyeCBgHnF_ge1RtHABENi84XDhxNpks5vg3Z5gaJpZM4cuxSP
.

Hi @felangel,

I have a question again on this. So listener is great but any event calls both the listener and the bloc builder right? My main issue is, suppose I have already loaded a list, now I want to display some error message in snackbar as user taps on some button. Because this new event rebuilds my widget as well and since this new state doesn't have a list I don't have a list to display in this new state.

Do I need to pass the list I loaded in every state that happens after the "ListLoadedState" state?

I hope I made myself clear in explaining the scenario and if I doing some stupid mistake here please let me know.

Hey @Sourabhlpu,

I think it might help if I better explain how it all works:

  1. An event is dispatched
  2. The bloc processes the event and yields a new state
  3. BlocListener gets notified of the state change

    • Here we can do one-time actions like navigate or show a SnackBar

  4. BlocBuilder gets notified of the state change

    • Here we need to tell Flutter what widgets to build in based on the current state

    • BlocBuilder can also get called at any time by Flutter to re-render the widgets so it should be pure

Regarding your particular scenario, if a feature can have an error and a list at the same time, then I would recommend having the state contain both the error as well as the list so that BlocBuilder renders the list while BlocListener shows a SnackBar. Hope that helps ๐Ÿ‘

Thanks for the quick response. That helped. I was thinking the same way
about the solution but I thought probably it was not the right thing to do
and there was a better way. ๐Ÿ˜Š

On Wed, Apr 17, 2019 at 11:51 PM Felix Angelov notifications@github.com
wrote:

Hey @Sourabhlpu https://github.com/Sourabhlpu,

I think it might help if I better explain how it all works:

  1. An event is dispatched
  2. The bloc processes the event and yields a new state
  3. BlocListener gets notified of the state change

  4. Here we can do one-time actions like navigate or show a SnackBar

  5. BlocBuilder gets notified of the state change

  6. Here we need to tell Flutter what widgets to build in based on the
    current state

  7. BlocBuilder can also get called at any time by Flutter to re-render
    the widgets so it should be pure

Regarding your particular scenario, if a feature can have an error and a
list at the same time, then I would recommend having the state contain both
the error as well as the list so that BlocBuilder renders the list while
BlocListener shows a SnackBar. Hope that helps ๐Ÿ‘

โ€”
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
https://github.com/felangel/bloc/issues/217#issuecomment-484205545, or mute
the thread
https://github.com/notifications/unsubscribe-auth/AEGYPSL7NAXOENBCKE3AIALPQ5TB5ANCNFSM4HF3CSHQ
.

No problem! Bloc has no opinion about how to set up your states and events so it's totally up to you and depends on the feature ๐Ÿ‘

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Reidond picture Reidond  ยท  3Comments

ricktotec picture ricktotec  ยท  3Comments

craiglabenz picture craiglabenz  ยท  3Comments

RobPFarley picture RobPFarley  ยท  3Comments

1AlexFix1 picture 1AlexFix1  ยท  3Comments