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
I have found the issue: it comes from redirect. For every
go
it will go on redirect first. Redirect is for condition of redirection, example: if user is not logged in => go to authentication.My suggestion to you is adding name parameter to GoRoute object. In your case the new GoRoute object will be as following
After changing it to example above you need to change your onPressed function to