简体   繁体   English

如何向 auto_route 1.0.0-beta.10 添加路由保护?

[英]How do I add a route guard to auto_route 1.0.0-beta.10?

Routes路线

@MaterialAutoRouter(
  routes: <AutoRoute>[
    AutoRoute(page: IntroView, initial: true),
    AutoRoute(page: LoginView),
    AutoRoute(page: MainView, guards: [AuthGuard])),
  ],
)
class $AppRouter {
  IntroView introView;
  LoginView loginView;
  MainView mainView;
}

The app throws the following error:该应用程序引发以下错误:

[ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: 'package:k2_flutter/navigation/app_router.gr.dart': Failed assertion: line 24 pos 53: 'authGuard != null': is not true.

In previous versions, this was set in ExtendedNavigator.在以前的版本中,这是在 ExtendedNavigator 中设置的。 The documentation is not clear.文档不清楚。

class AuthGuard extends AutoRouteGuard {
  @override
  Future<bool> canNavigate(List<PageRouteInfo> pendingRoutes, StackRouter router) async{
  return globalSharedPrefs.getString(PrefHelper.authToken) != null;
}
Widget build(BuildContext context) {
    appRouter = AppRouter(authGuard: AuthGuard());//pass it as a argument 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM