Skip to content

Instantly share code, notes, and snippets.

@subramanian42
Created May 17, 2023 15:47
Show Gist options
  • Save subramanian42/3a5dbd26c9c3de715f14bd795d1d5299 to your computer and use it in GitHub Desktop.
Save subramanian42/3a5dbd26c9c3de715f14bd795d1d5299 to your computer and use it in GitHub Desktop.
go_router redirect
class AppRouter {
static router(BuildContext context) => GoRouter(
debugLogDiagnostics: true,
routes: [
GoRoute(
path: '/',
builder: (context, state) => BlocProvider(
create: (context) =>
HomeBloc(RepositoryProvider.of<UserRepository>(context))
..add(FetchUserDetail()),
child: const HomeScreen(),
),
routes: [
GoRoute(
path: 'detail',
builder: (context, state) => BlocProvider(
create: (context) => RepositoryDetailBloc(
currentRepository: state.extra as GithubRepository),
child: const RepositoryDetailsScreen(),
),
),
],
),
GoRoute(
path: '/login',
builder: (context, state) => BlocProvider(
create: (context) =>
LoginBloc(RepositoryProvider.of<AuthRepository>(context)),
child: const LoginScreen(),
),
),
],
redirect: (context, state) {
final authStatus = context.read<AuthBloc>().state.status;
final bool loggingIn = state.matchedLocation == '/login';
if (authStatus == AppStatus.unauthenticated) {
return '/login';
}
if (loggingIn) {
return '/';
}
return null;
},
errorBuilder: (context, state) => const NotFoundScreen(),
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment