Hi Felix, I am currently learning bloc and have a question regarding the practice of using bloc. My question is that should one bloc handle different types of events?
For example, I have several CRUD operations to my database. So the events could be loading posts, adding posts, upvoting, adding comments, etc. These events could have corresponding states like LoadPostsSuccess/Failure, AddPostSuccess/Failure, etc. I am wondering whether I should use one single DBBloc to handle all these events/states or should I use LoadPostBloc, AddPostBloc, and UpvoteBloc to handle these?
Thank you in advance for your help!
Hi @shawnchan2014!
First of all, thanks for checking out the bloc packages and great question!
In order to answer your question I think I need to clarify a couple of concepts.
With all that being said, I think you need to ask yourself what is the state that is going to be output from these blocs.
Without any additional context, I would recommend having a single PostBloc which outputs a State which represents the posts at any given moments.
That PostBloc would then support events like CreatedPostButtonPressed and PostUpvotePressed and would use the repository/data provider to update the DB and then return the updated post state.
Let me know if that makes sense! My biggest piece of advice is to think about your application in terms of what interactions/events can happen and how that would affect the application/feature state.
Let me know if you have any more questions! 馃憤
Hi @felangel,
Thank you for your explanation! If I understand correctly, what you mean is to maintain the posts as the state and let PostBloc handle its changes, right? So each time an event like InitialializingPosts, CreatePostButtonPressed, or PostUpvotePressed is triggered, the PostBloc will return the updated list of posts in the Success state.
My question is that if I handle all the events in the PostBloc, then reacting to the Failure and Loading states for each of these events in the presentation layers would be less flexible. For example, I have multiple screens, one for presenting the list of posts (and probably upvoting the posts), one for creating the posts, and one for commenting on the posts. If a CreatePostFailure is yielded by PostBloc, then the screen that presents the list of posts will also get the CreatePostFailure state update instead of state updates that are only related to InitializingPosts and UpvotingPost.
Therefore, is there a good and flexible way to handle the above kind of situations in one single bloc? Or would it be more flexible to use different blocs to handle the local states on different screens or even create different blocs for each different events?
@shawnchan2014 Yes, you are correct in your understanding!
If you want to separate your application like you described then I'd recommend having a PostsListBloc, CreatePostBloc, PostCommentBloc to separate the state as you described.
In general, if you have different states you should maintain different blocs.
It's important to note that the CRUD database operations should not be in the blocs themselves but in a data provider which is reused by each bloc.
Does that help? Thanks!
Hi @felangel, thank you! I understand it now. About the CRUD database operations, I know I should put it in the data provider and wrap it in the repository layer. I think my wording in the question might cause some ambiguity. But still, thank you for reminding me!
Awesome! Glad I could help 馃槃
Most helpful comment
@shawnchan2014 Yes, you are correct in your understanding!
If you want to separate your application like you described then I'd recommend having a
PostsListBloc,CreatePostBloc,PostCommentBlocto separate the state as you described.In general, if you have different states you should maintain different blocs.
It's important to note that the CRUD database operations should not be in the blocs themselves but in a data provider which is reused by each bloc.
Does that help? Thanks!