Hi @felangel,
I have a question regarding getting data from the bloc. I have a PostListBloc
which accept an event LoadPostsRequested
and yield states like LoadPostsSuccess
, LoadPostsFailure
, etc. I know I can get the postList
from the repository and pass to the presentation layer via the LoadPostsSuccess
state. However, since I want to keep the list of posts shown in the presentation layer even when I received LoadPostsFailure
(e.g. when refreshing and getting an error, the list of posts should stay there), so instead of returning a Text Widget
in the middle of the screen to show the error, I want to keep the list and use a snackbar or dialog to show the error. Therefore, since the postList
comes from the state, if I want to keep the list, I will have to pass the postList
into both LoadPostsSuccess
and LoadPostsFailure
.
Another solution I came up with is not to pass the postList
into the state. Instead, the postList
is a variable in the bloc, and each time the bloc receive the event to load posts, the postList
variable is updated. Then, postList
is available to the presentation layer via a getter in the bloc. So in this way, the presentation layer can always display the list of posts by using _postListBloc.postList
and show the errors if it receives a failure state.
I have concerns with both solutions because the first solution has to pass postList
into LoadPostsFailure
(but LoadPostsFailure
itself is only related to errors) and the second solution is getting the list from the getter of the bloc instead of getting it from the state. Which solution do you think it's better? Or do you have any other suggestions?
Thank you in advance!
Hi @shawnchan2014 I think if you need to know what the list of posts is in the failure state as well as in the success state, I would recommend the base PostState
class to contain the list of posts so that you always have access to that data regardless of the current state.
The nice thing about bloc is that it has no opinion about how to represent states and events so you have full control to choose what works best for you 馃憤
I would love to hear which approach you end up going with.
Let me know if that makes sense or if you have any additional concerns and thanks for the great question!
Thank you for your response! So you mean either approach is OK with bloc right?
I am still trying and testing both approaches (both work for now). I will let you know if I have any further findings!
Yeah I think either approach is ok because at the end of the day the goal is to have business logic separated from presentation and have the business logic be easy to test and reuse.
Both approaches meet that definition so I would recommend you try out each and come back and share your findings so that others can benefit from them 馃憤
Again, great question and I look forward to hearing about which approach you decide to go with!
Closing for now but if you have any more questions/concerns feel free to comment and I'll gladly reopen this 馃槃