You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[go_router] Replace redirect with onEnter and onExit #102408
Would it be possible to cancel / delay the route change through these callbacks? I feel like this could be a good mechanism to prevent route changes in case, for instance, a user has unsaved changes on the page you might want to warn them about.
Reacted by GP4cK, m8811163008, Žiga Povhe and Daniel Wilianto
Yes, onExit would support this scenario, similar to how you would use WillPopScope. (WillPopScope doesn't work with page-backed routes, which built by the Router / go_router)
Reacted by Elte Hupkes, Alex Rankin, garrettApproachableGeek, Sergio Molchanovsky, Erfan Naderi, Fernando Gonzaga da Silva, GP4cK, Josue and HaredA
@chunhtai@johnpryan Hi guys, as we know, android app usually have a common requirement: when user on the root router, and then click the physical backward, will have a tips "click again to exist the app". previous navigator 1.0, i can use WillPopScope to listen & control the true/false to implement it, but i don't know how to implement it on the go router, any advice?
Yes, onExit would support this scenario, similar to how you would use WillPopScope. (WillPopScope doesn't work with page-backed routes, which built by the Router / go_router)
Is there any alternative to WillPopScope or a work-around? I'd love to warn my users that changes will be lost if he uses the Back-Button. Also I'd love to keep using go_router.
Reacted by Rydmike, garrettApproachableGeek, Sergio Molchanovsky, Ace Chen, Ahmed Fwela, Hadrami, Karlo Verde, Heech, Sebastian Podjasek, GP4cK and 19 more
Reacted by Godwin Mathias, Danylo Vus, Ekjyot Sandhu and MILLER/F
It's clean - you can open the routes.dart file and easily figure out, which screen uses which dependencies.
And it gives you scopes - the registered controller could be read from all the child routes.
Reacted by Ahmed Fwela, Mike Sies, Katekko, Tijani, Pablo Satler, Matti J. Frind, Attila Szabó, mori yuta, Krzysztof Kubicki, thomas-watchenterprise and 16 more
Reacted by Islam Akhrarov, 7Kronos, Artemis Kushner, RamiroGarrido, javi, felixmarrero, Rory Stephenson, Zdravko Čolić and martipello
Status update:
Top-level onEnter shipped in go_router 16.3.0 (PR #8339). It lets apps intercept incoming navigation, implement deep-link handling without page renders, and decide push vs go semantics before legacy redirects and route-level redirects run.
Reacted by Jakob Lichterfeld, Hayk Galstyan and Mark Videon
Status update: Top-level onEnter shipped in go_router 16.3.0 (PR #8339). It lets apps intercept incoming navigation, implement deep-link handling without page renders, and decide push vs go semantics before legacy redirects and route-level redirects run.
Unfortunately, the top level onEnter API does not fulfill the navigation prevention use-case (I followed up this issue from the Prevent Navigation topic of the package docs). Let me explain, while we can prevent from navigating from destination A to destination B, the prevention mechanism won't be executed in cascade. Here is an example:
Here are the edge-cases:
Suppose, we want to navigate from A/B/C to E/D/F, and underlying route B should have a navigation prevention mechanism but it won't be executed because the top-most route is C.
Suppose, we want to navigate from A/B to A/B/C, and the underlying route B should have a navigation mechanism but it will be executed as false-positive, because although we navigate from A/B to A/B/C, B is not expected to be removed.
The API that is the closest to implement navigation prevention in cascades is the onExit API but it still has this unresolved issue - #135909 , it requires disabling the swipe back gesture on iOS.
React Native's react-navigation package has navigation prevention mechanism where the callbacks are executed in cascades, I am wondering if we could expect similar API from the Go Router in nearest future?
Do I need to support both in my application? For instance, an authentication/onboarding flow may be triggered when an app launches via deeplink, but could also happen should a user logout or have their session expire. The former is best achieved with onEnter, but the latter can only be achieved with redirect. (IMO) the redirect pattern encourages string manipulation which is prone to error and even exploitation, and I'd rather use onEnter if I can, but the lack of statefulness makes that impossible for our use cases.
Perhaps I can work on what is needed at a minimal level for both cases, but this is frustrating given the time spent trying to debug how the lifecycle changes work :(
Activity
[-]Add hook for when routes are removed[/-][+]Add onExit callback[/+][-]Add onExit callback[/-][+]Replace redirect with onEnter and onExit[/+][-]Replace redirect with onEnter and onExit[/-][+][go_router] Replace redirect with onEnter and onExit[/+]ElteHupkes commentedon Sep 21, 2022
Would it be possible to cancel / delay the route change through these callbacks? I feel like this could be a good mechanism to prevent route changes in case, for instance, a user has unsaved changes on the page you might want to warn them about.
johnpryan commentedon Sep 22, 2022
Yes, onExit would support this scenario, similar to how you would use WillPopScope. (WillPopScope doesn't work with page-backed routes, which built by the Router / go_router)
AceChen1 commentedon Nov 25, 2022
@chunhtai @johnpryan Hi guys, as we know, android app usually have a common requirement: when user on the root router, and then click the physical backward, will have a tips "click again to exist the app". previous navigator 1.0, i can use WillPopScope to listen & control the true/false to implement it, but i don't know how to implement it on the go router, any advice?
otmi100 commentedon Nov 30, 2022
Is there any alternative to WillPopScope or a work-around? I'd love to warn my users that changes will be lost if he uses the Back-Button. Also I'd love to keep using go_router.
johnpryan commentedon Dec 1, 2022
We will probably stick with
redirectto avoid more breaking changes.@chunhtai also pointed out that this will require a framework change to make PopPageCallback asynchronous.
subzero911 commentedon Dec 21, 2022
+1 for onEnter / onExit methods in GoRoute.
It will allow us to register dependencies right inside GoRoutes (and generally run any custom code)
as proposed here: #99113 (comment)
It's clean - you can open the routes.dart file and easily figure out, which screen uses which dependencies.
And it gives you scopes - the registered controller could be read from all the child routes.
98 remaining items
flutter-triage-bot commentedon Jun 5, 2025
The
triaged-go_routerlabel is irrelevant if there is noteam-go_routerlabel orfyi-go_routerlabel.omar-hanafy commentedon Oct 26, 2025
Status update:
Top-level
onEntershipped in go_router 16.3.0 (PR #8339). It lets apps intercept incoming navigation, implement deep-link handling without page renders, and decide push vs go semantics before legacy redirects and route-level redirects run.kerimamansaryyev commentedon Dec 12, 2025
@chunhtai @omar-hanafy
Unfortunately, the top level
onEnterAPI does not fulfill the navigation prevention use-case (I followed up this issue from thePrevent Navigationtopic of the package docs). Let me explain, while we can prevent from navigating from destination A to destination B, the prevention mechanism won't be executed in cascade. Here is an example:Here are the edge-cases:
The API that is the closest to implement navigation prevention in cascades is the
onExitAPI but it still has this unresolved issue - #135909 , it requires disabling the swipe back gesture on iOS.React Native's
react-navigationpackage has navigation prevention mechanism where the callbacks are executed in cascades, I am wondering if we could expect similar API from the Go Router in nearest future?[go_router] Adds on exit (flutter#4699)
ThomasDuffinMears commentedon Jun 11, 2026
So toplevel
onEnter:And
redirect:refresh()orrefreshListenable.Do I need to support both in my application? For instance, an authentication/onboarding flow may be triggered when an app launches via deeplink, but could also happen should a user logout or have their session expire. The former is best achieved with
onEnter, but the latter can only be achieved withredirect. (IMO) the redirect pattern encourages string manipulation which is prone to error and even exploitation, and I'd rather use onEnter if I can, but the lack of statefulness makes that impossible for our use cases.Perhaps I can work on what is needed at a minimal level for both cases, but this is frustrating given the time spent trying to debug how the lifecycle changes work :(
[go_router] Adds on exit (flutter#4699)