felangel / bloc

A predictable state management library that helps implement the BLoC design pattern
https://bloclibrary.dev
MIT License
11.79k stars 3.39k forks source link

When we should use the Bloc in the flutter app? #702

Closed devmuaz closed 4 years ago

devmuaz commented 4 years ago

Hi, I've been working with Flutter and Bloc (especially) for a while.. and while I was working .. a couple questions came out and don't have the right or the appropriate solution..

Here are the questions came to me 1- When I should use the Bloc pattern design? I mean sometimes I just need one setState call...that's all.. should I forget the setState at all? like navigating between pages using Navigation Bar Widget or something like that.. or getting an image from device and display it inside an Image Widget

2- Is there any problem if I make more than Bloc in the app? I mean if I'm using some HTTP requests or Firebase for example... in mine I made [FirebaseAuthBloc, FirestoreBloc, FollowBloc, ThemeBloc, PostBloc]... in this way I found that each Bloc has less responsibilities and can be managed perfectly.. so is it a good way to use Bloc or there is a better way?

Note: in my case of app.. I'm using the AutomaticKeepAliveClientMixin to keep my page alive.. to prevent rebuild the widget when setState is called.

I don't know if my talk make sense but really I'm confused about it... that's why I'm asking here.

and Thanks in advance.

tenhobi commented 4 years ago

1) Basically, if the thing you want to do is part of business logic, you might want to do a bloc for it, so you can control it from your code. There might be cases like animations, some tabs, etc.

2) you SHOULD make more blocs, a basic rule is to make one bloc per big enough problem. And, if you do some "better" architecture (read Clean Architecture book, for example), you even may put out the business code from BLoCs to domain layer and use blocs only as the controller in the presentation layer. The benefit is that you can trully use the most of BLoC, and for example in your angulardart app, you can switch bloc with redux and everything works as supposed and you don't need to extract the logic code from the bloc implementation itself.

Do what best suits you. :-)

devmuaz commented 4 years ago

This is absolutely what I wanted So what I was doing is 75% right I guess .. and yeah I can use setState for small purposes, other than that Bloc is the best.. and yeah I read a little bit of Clean Architecture . Thank you so much for spending time to write this answer dear.

felangel commented 4 years ago

Hi @abdulmuaz97 👋 Thanks for opening an issue!

Thanks @tenhobi for the awesome answer! @abdulmuaz97 is it okay if we close this or did you have any additional questions?

devmuaz commented 4 years ago

No you're free to close it, I just needed some answers and @tenhobi gave me some.. Thank you.. in addition.. I'm looking forward for your answer too..

felangel commented 4 years ago
  1. You should use a bloc to separate business logic from your UI. In general navigation, animations, scrolling manipulation, etc... are purely presentational so they usually won’t require a bloc. A bloc should manage the state of a use-case (feature) and the UI (widget) should simply notify the bloc of events and react to state changes via BlocBuilder and BlocListener.

  2. Blocs should have a single responsibility. I would almost always recommend more/smaller blocs as opposed to fewer/larger blocs. Ideally your widgets should depend on one or more blocs, blocs should depend on one or more repositories, and repositories should depend on one or more data providers. You can read more about this at https://bloclibrary.dev/#/architecture.

Hope that helps 👍

Closing for now but feel free to comment with any additional questions and I’m happy to continue the conversation!

devmuaz commented 4 years ago

Actually you made the whole thing clear enough now... and that's what I was doing but since I was confused if this is the right way or not.. came here.. Thank you so much @felangel for spending time to write the answers, and lovely conversation with you all.