Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GO ROUTER IMPROVEMENT #77

Open
thaarrtt opened this issue Dec 10, 2024 · 1 comment
Open

GO ROUTER IMPROVEMENT #77

thaarrtt opened this issue Dec 10, 2024 · 1 comment

Comments

@thaarrtt
Copy link

instead building router manually and using uri string like

context.go("/home/product");

we can use go router builder to generate

our route like this

@TypedGoRoute<HomeScreenRoute>(
    path: '/',
    routes: [
      TypedGoRoute<SongRoute>(
        path: 'song/:id',
      )
    ]
)
@immutable
class HomeScreenRoute extends GoRouteData {
  @override
  Widget build(BuildContext context, GoRouterState state) {
    return const HomeScreen();
  }
}

@immutable
class SongRoute extends GoRouteData {
  final int id;

  const SongRoute({
    required this.id,
  });

  @override
  Widget build(BuildContext context, GoRouterState state) {
    return SongScreen(songId: id.toString());
  }
}

TextButton(
  onPressed: () {
    const SongRoute(id: 2).go(context);
  },
  child: const Text('Go to song 2'),
),

also more scalable approach to router builder config

@thaarrtt
Copy link
Author

instead handling all redirect globally

we can manage redirection per route basis, more code in short term but easier route handling in the future when the app gets too big/complicated

GoRouter(
          routes: [
            GoRoute(
              path: '/',
              builder: (context, state) => const HomePage(),
            ),
            GoRoute(
              path: '/secret-route',
              redirect: (context, state) {
                final user = app_state.cachedAuthedUser.of(context);

                if (user == null) {
                  return Uri(
                    path: '/auth',
                    queryParameters: {
                      // Note that this requires usePathUrlStrategy(); from `package:flutter_web_plugins/url_strategy.dart`
                      // and set
                      OidcConstants_Store.originalUri: state.uri.toString(),
                    },
                  ).toString();
                }
                return null;
              },
              builder: (context, state) => const SecretPage(),
            ),
            GoRoute(
              path: '/auth',
              redirect: (context, state) {
                //
                final user = app_state.cachedAuthedUser.of(context);
                if (user != null) {
                  final originalUri = state
                      .uri.queryParameters[OidcConstants_Store.originalUri];
                  if (originalUri != null) {
                    return originalUri;
                  }
                  return '/secret-route';
                }
                return null;
              },
              builder: (context, state) => const AuthPage(),
            ),
          ],
        ),

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant