I am migrating my project from 4.XX into the latest version now of auto_route package ^7.7.1 and what I got broke are the guards, before we passed it in the AppRouter( ...guards ) as prams and now that is not possible
So from the documentation, I found that I have to do it on the AppRouter class but how to pass the context or a Stream instance like bloc to that class, the documentation is not clear for that at all, and no real-world sample
So before it was like that
_router = AppRouter(
fingerPrintGuard: FingerPrintGuard(
BlocProvider.of<SettingsCubit>(context),
),
);
Now I did the following, but it throws an error for missing argument ( the settings bloc )
@AutoRouterConfig(replaceInRouteName: 'Page,Route')
class AppRouter extends $AppRouter {
@override
List<AutoRoute> get routes => [
AutoRoute(
initial: true,
page: TabsRoute.page,
guards: [
FingerPrintGuard( ) //
],
children: [
// children
],
),
];
}
From the doc, I found that I have to pass the stream like this in the router.config()
, so I did but I don't know what next and how to retrieve it from the guard
MaterialApp.router(
routerConfig: _router.config(
reevaluateListenable: ReevaluateListenable.stream(
context.watch<SettingsCubit>().stream)),
),
Also here's my guard
class FingerPrintGuard extends AutoRouteGuard {
final SettingsCubit _settingsCubit;
FingerPrintGuard(this._settingsCubit);
@override
void onNavigation(NavigationResolver resolver, StackRouter router) async {
// _authCubit.state.authenticated
// ? resolver.next()
// : router.replace(const FingerPrintRoute());
resolver.next();
if (!_settingsCubit.state.authenticated) {
router.push(const FingerPrintRoute());
}
}
}