skip to Main Content

i’m starting using go router, i made some tests and it was working well.
Now i tried to encapsulates multiples routes. To have for example: /welcome/authentication

So here is my router:

final router = GoRouter(
  initialLocation: AppRoutes.root,
  redirect: (context, state) => AppRoutes.welcome,
  routes: [
    GoRoute(
      path: AppRoutes.welcome,
      builder: (context, state) => const WelcomePage(),
      routes: <RouteBase>[
        GoRoute(
          path: AppRoutes.authentication,
          builder: (context, state) => const AuthenticationIntraPageWebView(),
        ),
      ],
    ),

So we got route /welcome and in it authentication.

So when app run, i’m on welcome page. But when i want to redirect to authentication by using:

onPressed: () {
                      print('go to ${'${AppRoutes.welcome}/${AppRoutes.authentication}'}');
                      try {
                        context.go(
                          '${AppRoutes.welcome}/${AppRoutes.authentication}',
                        );
                        print('redirection done');
                      } catch (e) {
                        print('error navigation: $e');
                      }
                    },

It just say:
go to /welcome/authentication redirection done

I tried to seperate and it work but i want to have the benefits of encapsuled routes.

I also tried to simplify to test:

redirect: (context, state) => '/a',
  routes: [
    GoRoute(
      path: '/a',
      builder: (context, state) => const WelcomePage(),
      routes: <RouteBase>[
        GoRoute(
          path: 'b',
          builder: (context, state) => const AuthenticationIntraPageWebView(),
        ),

and call it like so:

onPressed: () {
                      print('test redirection');
                      context.go(
                        '/a/b',
                      );
                    },

It still don’t redirect.
Why it don’t work please ?
Have a nice day

2

Answers


  1. Chosen as BEST ANSWER

    I have found the issue: it comes from redirect. For every goit will go on redirect first. Redirect is for condition of redirection, example: if user is not logged in => go to authentication.


  2. My suggestion to you is adding name parameter to GoRoute object. In your case the new GoRoute object will be as following

    GoRoute(
              path: AppRoutes.authentication,
              name: 'authentication' // Can't start with /
              builder: (context, state) => const AuthenticationIntraPageWebView(),
            ),
    

    After changing it to example above you need to change your onPressed function to

     try {
                                context.goNamed(
                                  'authentication',
                                );
                                print('redirection done');
                              } catch (e) {
                                print('error navigation: $e');
                              }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search