wamae
wamae

Reputation: 840

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. 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;
}

Upvotes: 0

Views: 1267

Answers (1)

charmy
charmy

Reputation: 31

Widget build(BuildContext context) {
    appRouter = AppRouter(authGuard: AuthGuard());//pass it as a argument 
}

Upvotes: 1

Related Questions