我正在使用ShellRoute来控制BottomNavigationBar。它工作正常。但是当我尝试context.go
时,我得到的消息是没有找到给定的路径。
static final _rootNavigatorKey = GlobalKey<NavigatorState>();
static final _shellNavigatorKey = GlobalKey<NavigatorState>();
static final GoRouter router = GoRouter(
initialLocation: AppRoutes.home,
debugLogDiagnostics: true,
navigatorKey: _rootNavigatorKey,
routes: [
ShellRoute(
navigatorKey: _shellNavigatorKey,
builder: (context, state, child) {
return BlocProvider(
create: (context) => NavigationCubit(),
child: MainScreen(child: child),
);
},
routes: [
GoRoute(
parentNavigatorKey: _shellNavigatorKey,
path: AppRoutes.home,
pageBuilder: (context, state) => const NoTransitionPage(
child: HomeScreen(),
),
routes: [
GoRoute(
parentNavigatorKey: _shellNavigatorKey,
path: AppRoutes.homeDetails,
builder: (context, state) => const HomeDetailsScreen(),
),
],
),
GoRoute(
parentNavigatorKey: _shellNavigatorKey,
path: AppRoutes.profile,
pageBuilder: (context, state) => const NoTransitionPage(
child: ProfileScreen(),
),
routes: [
GoRoute(
parentNavigatorKey: _shellNavigatorKey,
path: AppRoutes.profileDetails,
builder: (context, state) => const ProfileDetailsScreen(),
),
],
),
GoRoute(
parentNavigatorKey: _shellNavigatorKey,
path: AppRoutes.settings,
pageBuilder: (context, state) => const NoTransitionPage(
child: SettingsScreen(),
),
),
],
),
],
errorBuilder: (context, state) => const ErrorScreen(),
);
字符串
当我尝试从AppRoutes.home
转到AppRoutes.homeDatails
时:
context.go(AppRoutes.homeDetails),
型
我得到了结果:
[GoRouter] No initial matches: details
型
我尝试添加parentNavigatorKey
,但它没有帮助. GoRouter.of(context).go(AppRoutes.homeDetails)
也不工作.
1条答案
按热度按时间bqjvbblv1#
我遇到了同样的问题,这是对我起作用的:
如果您在AppRouter中保留所有路径,以便从应用程序的不同屏幕访问它们,请确保它们是完整路径,如:
字符串
但是,在GoRouter对象中,您应该使用短路径,而不使用“/”表示子路由,如:
型
这样,您就可以使用context.go(AppRoutes.homeDetails),并且它应该可以按预期工作!