Bloc: [Question] Navigate within BlocListener or Bloc

Created on 2 Jan 2020  路  2Comments  路  Source: felangel/bloc

First of all congratulations for the excellent work with package flutter_bloc.

I would like to hear from you what you think is architecturally best for navigation with flutter_bloc. I always find myself with two possibilities:
1) Navigate within a BlocListener
2) Navigate within Bloc (using a global navigatorKey).

I believe the recommended way to navigate flutter_bloc is through BlocListener, but I constantly find myself having to create an extra state just to navigate to page X.

For example, assuming I have an action on the UI that triggers the NavigateToPageX event, I would have to create a PageXNavigationRequested state, and in BlocListener, by checking this state I would navigate to PageX. I find this a little unnecessary boilerplate and most of the time I would prefer to add the NavigateToPageX event and inside my bloc I check for this event and navigate inside Bloc (since I don't need the context because I'm using navigatorKey).

My question is, is this a poor design or is it somehow acceptable?

I would like to hear from you.

I'm using this package in some projects and its great. Thanks again for you work and effort.

question

Most helpful comment

Hello @felangel thanks for your fast reply.

I think i was over engineering a solution that's not needed at all. I reviewed my code and i really don't need the extra states i was creating, so i just followed your advice and navigated directly from the UI.

You can close this one, since i have already got the answer i need :)

Thanks again!

All 2 comments

Hi @rodrigobastosv 馃憢
Thanks for opening an issue and for the positive feedback!

As you mentioned, I would highly recommend keeping navigation in the UI via BlocListener (option 1) because with option 2 you are tightly coupling your bloc to the UI layer (ideally there should be no Flutter-specific code or imports in your bloc files).

I would love to hear more about the specific use-case that you have because if you find yourself having to add states to do the navigation then you likely don't need to use bloc for navigation at all. I would only recommend using BlocListener to handle navigation when the navigation occurs conditionally based on business logic. If you're just navigating in response to user interaction you shouldn't use a bloc and can just do the navigation directly in the onPress callback.

Hope that helps! 馃憤

Hello @felangel thanks for your fast reply.

I think i was over engineering a solution that's not needed at all. I reviewed my code and i really don't need the extra states i was creating, so i just followed your advice and navigated directly from the UI.

You can close this one, since i have already got the answer i need :)

Thanks again!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

1AlexFix1 picture 1AlexFix1  路  3Comments

RobPFarley picture RobPFarley  路  3Comments

hivesey picture hivesey  路  3Comments

ricktotec picture ricktotec  路  3Comments

clicksocial picture clicksocial  路  3Comments