我开始使用 go router,我做了一些测试,它运行良好。 现在我尝试封装多个路由。例如:
/welcome/authentication
这是我的路由器:
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(),
),
],
),
因此我们得到了路由 /welcome 并在其中进行了身份验证。
因此,当应用程序运行时,我位于欢迎页面。但是当我想使用以下方法重定向到身份验证时:
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.
Why it don't work please ?
Have a nice day
我给你的建议是向 GoRoute 对象添加 name 参数。在您的情况下,新的 GoRoute 对象将如下
GoRoute(
path: AppRoutes.authentication,
name: 'authentication' // Can't start with /
builder: (context, state) => const AuthenticationIntraPageWebView(),
),
将其更改为上面的示例后,您需要将 onPressed 函数更改为
try {
context.goNamed(
'authentication',
);
print('redirection done');
} catch (e) {
print('error navigation: $e');
}