Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Recommendation when using bloc pattern in flutter

Tags:

flutter

bloc

When using flutter bloc what is the recommendation, is it recomended for each page to have its own bloc or can i reuse one block for multiple pages, if so how?

like image 360
user2293554 Avatar asked Oct 19 '25 03:10

user2293554


1 Answers

I think that the best solution is to have one BLoC per page. It helps you to always know in which state each screen is just by looking at its BLoC. If you want to show the state of each of your tabs independently you should create one BLoC for each tab, and create one Repository which will handle fetching the data. But if the state of every tab will be the same, (for example you fetch data only once for all of the screens, so you don't show loading screen on every tab) then I think that you could create just one BLoC for all of this tabs.

It is also worth to add, that BLoCs can communicate with each other. So you can get state of one BLoC from another, or listen to its state changes. That could be helpful when you decide to create separate BLoCs for tabs.

I have addressed this topic in my latest article. You can check it out if you want to dive deeper.

like image 190
Kacper Kogut Avatar answered Oct 22 '25 04:10

Kacper Kogut