-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Side-effects (events) without overriding state #812
Comments
Hi @chriswiesner 👋 Several things I would recommend considering are:
class WeatherState {
final Weather weather;
final bool hasError;
...
} Then you can add a copyWith to your state and yield Of course you can always add another stream for side effects but in my opinion that adds a significant amount of additional complexity which you can usually avoid if you structure your states properly. Hope that helps 👍 |
Thanks for the fast response.
|
No problem! I wouldn't recommend a NavigationBloc because again blocs should not have any dependency on Flutter or the UI. I would recommend handling navigation within a BlocListener in cases where you have business logic dependent navigation. Are you able to share a link to a sample app which illustrates the problems you're facing? It would be much easier for me to give suggestions for a concrete sample app. Thanks! |
There would be no dependency to flutter imho. It's just issuing events for the UI like NavigateTo(). Handling navigation with BlocListener would imply that I have a I'll try to create a sample app. |
I don't think the extra property should be necessary. Awesome! Don't spend too much time on the sample...just throw something super simple that mimics your current setup and what you're trying to accomplish 👍 |
here is the sample repo: https://github.com/chriswiesner/blocSample/blob/master/lib it's about a listview where you can "assign" items by swiping left/right. Another similar case for side-effects I came across now was saving an entity. You press a save button. the Bloc saves the entity but I want to listen for the result of the saving process. So if it fails I'll stay on the screen (having the entity still displayed) - but if it succeeds it should close the screen. |
@chriswiesner thanks for putting together the sample! I'll have a look later today and will try to open a pull request with any suggestions 👍 |
@chriswiesner I opened a pull request with some suggestions. I'm sure I can clean it up even further if I had a better understanding of the requirements but hopefully it gives you a good sense of how to structure things. Closing for now but feel free to comment with additional questions and I'm happy to continue the conversation 👍 |
Thanks for taking the time and the suggested solution. (I commented my thoughts) The one issue I'm now having is that the ListItem data is changing when I assign an item. (formerly I updated the whole list, which was anyhow not the best approach). |
also the items are used in a "grouped" list - where the unassigned items are grouped together in a section and once it is assigned not only the item data is update, also the list of items is changing as it is now in the grouped section. therefore I need to update the list of items if an assignment of an item is successful. would i listen for the assignSuccess and then issue a refresh on the listBloc? |
No problem! I would recommend holding the the view model for the ListItem in the ListItemBloc's state.
Not sure what you mean. You shouldn't need to emit additional events just to rebuild items.
I would manage the groups via the state of the list bloc. You can add a GroupChanged event to the ListBloc when you assign a ListItem. Hope that helps 👍 |
Thanks, yes for the first point It seemed a bit wrong to me to always pass the data/state (viewmodel) with the event (e.g. For the second point I ended up as you suggested, to emit a updateItem event to the list bloc. Thanks again taking the time! |
Hi,
I'm having a Bloc for a list with following states:
Empty
,Loading
,Loaded(List<> data)
,Error(errormessage)
Adding side-effects with
BlocListener
has the disadvantage of overriding the actual "data" state.For example I want to issue side-effects from my Bloc (like Navigating to detail screen, showing snackbar).
This yields a state
NavigateToDetail(...)
which is overriding theLoaded()
state and my list would be empty when I come back from the detail.For navigation I guess I should use a global Bloc handling the navigation, but the issue still remains for the Snackbar use-case.
Wouldn't it be better to separate the side-effects from the actual (data)state of the Bloc?
I guess I'd need to use a separate Stream for my side-effects. But then listening/disposing in the UI is a bit verbose.
Are there any recommended solutions?
The text was updated successfully, but these errors were encountered: