![](/img/trans.png)
[英]Flutter auto_route | How do I wrap a route with BlocProvider?
[英]How do I add a route guard to auto_route 1.0.0-beta.10?
路線
@MaterialAutoRouter(
routes: <AutoRoute>[
AutoRoute(page: IntroView, initial: true),
AutoRoute(page: LoginView),
AutoRoute(page: MainView, guards: [AuthGuard])),
],
)
class $AppRouter {
IntroView introView;
LoginView loginView;
MainView mainView;
}
該應用程序引發以下錯誤:
[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.
在以前的版本中,這是在 ExtendedNavigator 中設置的。 文檔不清楚。
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.