Hello everyone, i'm currently learning how to use state management in flutter, and I decided to use Bloc for this task. I have a small question, I have to use normal http request and listen to a webSocket, i need a recommendation from someone that is better informed than me at using bloc.
Should i have a different bloc for wsEvents or should i just have a single bloc for "user" and wsEvents. In my opinion the best thing is to have a different bloc with all the event management there. But I have no idea how to pass states from one bloc to another.
Every little bit of help is appreciated!
Hi @AbeOrigin 馃憢
It depends on what kind of data you're receiving on the websocket and where you need to use it.
You can definitely expose your websocket stream through a repository and consume it in the bloc of your choice.
If you want a more advanced guideline you should provide more details about your websocket usage.
Hi, thank you for the response !!! 馃槂 .
The socket will send events that will change the interface, for example new message, you have been added to a group, X person is writing to you, they are not only user things, so that why I think making a new bloc for it.
I currently have a websocket handler, and my idea was to give this handler the ability to send events to different blocs based on the type of websocket event I get. I was thinking of doing it like this to be more organized and keep the socket out of any bloc.
If this is any help im implementing Mattermost api to a Flutter app.
Again thank you very much @RollyPeres for your help.
Hi @AbeOrigin 馃憢
Thanks for opening an issue!
I would recommend writing a standalone Dart package which wraps the Mattermost API and exposes the Stream of events. Then you can have one or more repositories consume the api client and the bloc will as usual interact with one or more repositories.
Hope that helps!
Closing for now but feel free to comment with any additional questions or if you haven't already join us on discord to continue the conversation 馃憤
@felangel 's solution sums up well a good approach to your case. You can additionally filter your stream of events in different repositories to only get certain events which are relevant to it. You'd still end up listening to streams of events in your blocs.
Most helpful comment
Hi @AbeOrigin 馃憢
It depends on what kind of data you're receiving on the websocket and where you need to use it.
You can definitely expose your websocket stream through a repository and consume it in the bloc of your choice.
If you want a more advanced guideline you should provide more details about your websocket usage.