Why use BloC or Provider in Flutter when we already have Flutter's built-in setState?
Asked Answered
C

2

5

Maybe I don't understand the purpose of BloC or Provider but I'm confused as to why we would ever want to use them instead of using Flutter's built-in state management using the Stateful widget. I've just finished an app and can't remember a point where I wished I needed something more than the defaults. Can anyone clear things up for me?

Choochoo answered 13/10, 2022 at 0:33 Comment(2)
setState is for local state. state managment is for state that is shared between widgets.Fussell
@RandalSchwartz ah, I see.Choochoo
S
3

There are some case that you need BLoC to make you easier to define each state or condition that happening inside the application.

We will start discussing creating app like https://www.tokopedia.com/ (inspect element and go with phone size preview). You will see that between widget section tokopedia_ss there are some loading animations, and when the data load complete the widget loading animation changed to viewable widget (as user).

in bloc, you will make stateLoading(), stateComplete(data), stateFailed(data). and in the controller or screen you can describe what will happen when bloc state is stateLoading etc...

Creating this case with setstate is more complicated and make your code messy, also setstate will make phone render all the Build() code. not like BloC builder, you can define each widget or section.

So, when there are 10 sections, using Bloc you able to make it render each state but when using standard setstate it will render all the 10 sections in one time every state changes.

More information about BloC: article_about_BloC

Scant answered 13/10, 2022 at 5:26 Comment(0)
P
3

There are a few reasons to use a BloC or Provider rather than Flutter's built-in setState:

  1. BloC and Provider offer a more robust way to manage state.

  2. BloC and Provider make it easier to update state across multiple widgets.

  3. BloC and Provider can be used to manage async data.

  4. BloC and Provider offer a more modular way to structure your code.

Poach answered 13/10, 2022 at 0:37 Comment(1)
Thanks, now that I see this list, I recognize times when I wished my code could be structured a bit better.Choochoo
S
3

There are some case that you need BLoC to make you easier to define each state or condition that happening inside the application.

We will start discussing creating app like https://www.tokopedia.com/ (inspect element and go with phone size preview). You will see that between widget section tokopedia_ss there are some loading animations, and when the data load complete the widget loading animation changed to viewable widget (as user).

in bloc, you will make stateLoading(), stateComplete(data), stateFailed(data). and in the controller or screen you can describe what will happen when bloc state is stateLoading etc...

Creating this case with setstate is more complicated and make your code messy, also setstate will make phone render all the Build() code. not like BloC builder, you can define each widget or section.

So, when there are 10 sections, using Bloc you able to make it render each state but when using standard setstate it will render all the 10 sections in one time every state changes.

More information about BloC: article_about_BloC

Scant answered 13/10, 2022 at 5:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.