0

In StackOverflow, you can access question through two different URL

I am trying to build the Same.

  GoRoute(
            path: "${LeaderboardItemPage.route}/:creationId",
            name: LeaderboardItemPage.route,
            redirect: (
              context,
              state,
            ) {
              final creationId = state.pathParameters['creationId'];
              return "${LeaderboardItemPage.route}/$creationId/overview";
            },
            routes: [
              /// same as a
              GoRoute(
                path: ":slug",
                // pag
                // name: LeaderboardItemPage.route,
                builder: (BuildContext context, GoRouterState state) {
                  final creationId = state.pathParameters['creationId'];
                  final slug = state.pathParameters['slug'];
                  print("Slug: $slug");
                  if (creationId == null) {
                    return ErrorWidget();
                  }
                  return LeaderboardItemPage(
                    creationId: creationId,
                  );
                },
              ),
            ],
          ),

This barely works, but has lots of issue. First of all , I am forcing a dummy slug called overview . What if I actually want it to be null and show a popup to the user asking to create a permalink ?

Secondly,

 context.pushNamed(LeaderboardItemPage.route, pathParameters: {
      'creationId': leaderboardItem.creationId,
      'slug' : "some-slug"
    });
    return;

I get unknown param "slug" for /creations/:creationId. I want to use the same routeName for both and still use the pathParameters, not direct url

3
  • Since you want two different links to point to the same location, why not have a GoRoute that can only be visited by the first link; then in your GoRouter, not GoRoute, make use of the redirect so that when the second link is detected it will redirect to the first. Commented Jul 27, 2024 at 14:08
  • Isn't that what I am doing already ? redirect: ( context, state, ) { final creationId = state.pathParameters['creationId']; return "${LeaderboardItemPage.route}/$creationId/overview"; }, Commented Jul 27, 2024 at 17:58
  • Handle the redirect in the GoRouter level, not GoRoute @TSR Commented Jul 28, 2024 at 4:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.