I have an application where the user needs to authenticate in order to have access. I am using the bloc library for state managing the state. My AuthenticationBloc
holds a status streamed through the authentication repository
. So if any event happens that changes the status the Bloc
is aware of it. I took the example code as reference.
Now i want to use go_router
for navigating. This is my SplashScreen Route:
GoRoute(
path: KiGoRouter.splash,
builder: (BuildContext context, GoRouterState state) {
return BlocListener<AuthenticationBloc, AuthenticationState>(
listener: (BuildContext context, AuthenticationState state) {
if (state.status == AuthenticationStatus.authenticated) {
GoRouter.of(context).go(KiGoRouter.home);
} else if (state.status == AuthenticationStatus.unauthenticated) {
GoRouter.of(context).go(KiGoRouter.login);
}
},
child: Center(
child: CircularProgressIndicator(),
),
);
}),
When the app starts and the user is unactuhenticated it redirects as it should and displays the login page. Now when the user enters the credentials and the AuthenticationBloc
status changes to authenticated nothing happens. It stays on the login screen.
I even tested to write the code inside the redirect of GoRouter
but its the same behaviour. It seems like inside of GoRouter
the Bloc
is not updated.
I habe used get_it
to inject the AuthenticationBloc
as a singlton
and provided it before the construction of the MaterialApp.
Even when using context.push(x) to prevent the Listener from being removed from the widget tree, the behavior stays the same.
Has anyone a solution or hint on this?
UPDATE
This is my injection of the AuthenticationBloc
using get_it:
locator.registerLazySingleton(() => AuthenticationBloc(
authenticationRepository: locator<AuthenticationRepository>(),
userRepository: locator<UserRepository>(),
getUserDataUseCase: locator()));
And before the build of the MaterialApp
, I am providing the singleton as a value:
BlocProvider.value(value: di.locator<AuthenticationBloc>()..add(AppStartup()))