gpt4 book ai didi

Flutter:Autoroute:RouteGuard 在 AutoTabsScaffold 中不工作

转载 作者:行者123 更新时间:2023-12-05 08:03:36 27 4
gpt4 key购买 nike

我正在尝试为我的 AutoTabsScaffold [bottom nav] 添加 auth guard 但它不起作用,它在其他导航页面中工作但不只是在我的登录页面中 [其中 AutoTabsScaffold|底部导航位于] 我在这里遗漏了什么吗?

使用:auto_route:^3.2.4 [pub.dev]

登陆页面类

   return AutoTabsScaffold(

backgroundColor: Theme
.of(context)
.scaffoldBackgroundColor,
routes: const [
HomeRouter(),
CategoryRouter(),
OrderRouter(),
ProfileRoute(),
],
bottomNavigationBuilder: (_, tabsRouter) {return}

类认证守卫:

class RouteGuard extends AutoRedirectGuard {
final AuthService authService;

RouteGuard(this.authService) {
authService.addListener(() {
if (!authService.authenticated) {
// should be called when the logic effecting this guard changes
// e.g when the user is no longer authenticated
reevaluate();
}
});
}

@override
void onNavigation(NavigationResolver resolver, StackRouter router) {
if (authService.authenticated) return resolver.next();
router.push(
LoginRoute(
onLoginCallback: (_) {
resolver.next();
router.removeLast();
},
),
);
}
}

路由器.dart

@MaterialAutoRouter(
replaceInRouteName: 'Page|Screen,Route',
routes: <AutoRoute>[
AutoRoute(page: OrderPreviewPage),
AutoRoute(page: AddCardPage,),
AutoRoute(page: PaymentPage,),
AutoRoute(page: SplashPage, initial: true),
AutoRoute(page: MyCartPage,),
AutoRoute(page: IntroPage),
AutoRoute(page: RegisterPage),
AutoRoute(page: ProductDetailPage),
AutoRoute(page: ProductListingPage),
AutoRoute(page: CartSummaryPage,),
AutoRoute(page: LoginPage, path: 'login'), //name: 'LoginRoute'
AutoRoute(
initial: true,
page: LandingPage,

path: 'landing',
children: [
AutoRoute(
path: '',
name: 'homeRouter',
page: HomePage,
),
AutoRoute(
path: 'category',
name: 'categoryRouter',
page: CategoryPage,
),
AutoRoute(
path: 'orders',
name: 'orderRouter',
page: OrdersPage,
),
AutoRoute(
path: 'profile',
guards: [RouteGuard], //<------- here registered router guard but not working in Tabscaffold page
page: ProfilePage,
),
// RedirectRoute(path: '*', redirectTo: ''),

],
),
],
)

尝试在底部导航中添加路由守卫,但它没有像我预期的那样工作。我在这里缺少什么?

最佳答案

我有一个类似的结构,对我来说,它的工作原理是在你着陆的情况下在嵌套导航的顶部添加守卫:

AutoRoute(
initial: true,
page: LandingPage,
guards: [RouteGuard],
path: 'landing',

关于Flutter:Autoroute:RouteGuard 在 AutoTabsScaffold 中不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72013662/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com