Bloc: Need help for creating a common Network Handler

Created on 28 Aug 2019  路  6Comments  路  Source: felangel/bloc

Hello Felangel,
I am migrating from the MVP pattern to flutter bloc, I have created a Network Handler class where it will return the response depending on the various requests,
Requirement : depending upon the response, i have to show the snackbar or navigate to another screen,
if success then Navigate to Home (can be any page..)
if failure then show snackbar

How could I achieve this?
Below is the link of my project
https://github.com/sanketwarule/account_opening_app

question

Most helpful comment

Thank you @felangel for your valuable time

All 6 comments

Hi @sanketwarule 馃憢
Thanks for opening an issue!

Have you taken a look at the flutter login example as well as the SnackBar Recipe and Navigation Recipe?

Those should help 馃憤

Thank you for your response @felangel ,
Got the solution in your recipe, Thank you @felangel for your help, Your documentation is very nice.

Hi @felangel sorry to disturb you again, but I have updated the code, can you please check whether I am on the right track or not. What can be done to optimise the code in UI layer, like callback functions for success and error in NetworkBloc instead of checking the states in UI layer(in BlocListener), but the issue is what can be returned for snackbar -> Widget and for Navigation -> ..., Can I assign a dynamic instead of State.
Correct me if I am wrong, as I am on the learning stage
Thank you

Hi @sanketwarule I took a look and I would recommend not having a NetworkHandler because you are currently passing the BuildContext into it which is coupling the networking layer with the UI layer. Instead, I would recommend creating an http client which you inject into repositories. Then each repository can make requests to your apis and the blocs can use the repositories to convert events into states.

I would recommend taking a look at the architecture docs for more details.

Hope that helps 馃憤

Thank you @felangel for your valuable time

Hello @felangel , sorry to disturb you again, As per your suggestion I have added a repository between the Api(NetworkHandler) and Blocs(LoginBloc) i.e LoggedInUserRepository, Now my doubt is From where I can dispatch the Network events so that I could able to navigate to the Home Page or show the snackbar...
You can run the code as it is...
I have wrote the doubts in the Login Page(inside pages directory)
You can modify my code so that I can understand(Hint will also be helpful)

Below is the link of my project
https://github.com/sanketwarule/account_opening_app

Was this page helpful?
0 / 5 - 0 ratings