csells / go_router

The purpose of the go_router for Flutter is to use declarative routes to reduce complexity, regardless of the platform you're targeting (mobile, web, desktop), handling deep linking from Android, iOS and the web while still allowing an easy-to-use developer experience.
https://gorouter.dev
441 stars 96 forks source link

Redirect on pop back #348

Closed marcguilera closed 2 years ago

marcguilera commented 2 years ago

This may be intended but I noticed that when going back the redirect is not evaluated. Consider the following example:

GoRoute(
  name: AuthRoutes.auth,
  path: '/auth',
  builder: (c, s) => const CommonAuthPage(),
  redirect: (s) {
    if (s.name != AuthRoutes.forgotPassword && reader($device) == Device.mobile) {
      return s.namedLocation(AuthRoutes.authSections, params: {'section': 'login'});
    }
  },
  routes: [
    GoRoute(
      name: AuthRoutes.authSections,
      path: ':section(login|register)',
      builder: (c, s) => AuthTabs(initialSection: state.params['section']!),
      redirect: (s) {
        if (reader($device) != Device.mobile) {
          return s.namedLocation(AuthRoutes.auth);
        }
      }
    ),
    GoRoute(
      name: AuthRoutes.forgotPassword,
      path: 'recover',
      builder: (c, s) => const ForgotPage(),
    )
  ],
)

In the example device represents the size of the screen for which I have slightly different designs. On mobile login and register will be different tabs (/auth/login or /auth/register) in a PageView while on other form factors they both can go side by side (just /auth).

With the above code, if I type /auth/recover in my browser with a small screen the forgot screen is stacked on top of /auth as one would expect. However, when i tap the back button I go to /auth so there is no redirect.

The question is: should the redirect in a route be evaluated when popping or is this the intended behaviour.

csells commented 2 years ago

The redirect will not fire in the case of a pop. You probably want to set a refreshListenable according to the redirection docs.