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
442 stars 96 forks source link

Named route from redirect guard #69

Closed oravecz closed 2 years ago

oravecz commented 2 years ago

The decision was made to return a route's path from the redirect guard function. It would have been convenient to expose context to the redirect function so the user can choose to go/push a path/named route.

csells commented 2 years ago

fixed in v2.0.0. @oravecz please verify.

oravecz commented 2 years ago

Works well. It took me a while to notice the API to use (state.namedRoute("route_name")), but I got there in the end.

Here is a test that is closest to my use case (with the redirect function not on the top-level router.

  test('sub-level redirect w/ named routes', () {
    final routes = [
      GoRoute(
        name: 'home',
        path: '/',
        pageBuilder: (builder, state) => HomePage(),
        routes: [
          GoRoute(
              name: 'dummy',
              path: 'dummy',
              pageBuilder: (builder, state) => DummyPage(),
              redirect: (state) => state.namedLocation('login'),
          ),
          GoRoute(
            name: 'login',
            path: 'login',
            pageBuilder: (builder, state) => LoginPage(),
          ),
        ],
      ),
    ];

    final router = GoRouter(
      initialLocation: '/dummy',
      debugLogDiagnostics: true,
      routes: routes,
      errorPageBuilder: _dummy,
    );
    expect(router.location, '/login');
  });
csells commented 2 years ago

glad to here it works for you. it was a great feature suggestion.