Flutter go_router与块身份验证重定向不工作

oxiaedzo  于 2023-05-19  发布在  Flutter
关注(0)|答案(1)|浏览(156)

我有一个应用程序,用户需要进行身份验证才能访问。我正在使用块库来管理状态。我的AuthenticationBloc持有通过authentication repository传输的状态。因此,如果发生任何改变状态的事件,Bloc都会知道。我把示例代码作为参考。
现在我想使用go_router进行导航。这是我的SplashScreen路线:

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(),
              ),
            );
          }),

当应用程序启动并且用户未被激活时,它会按照应该的方式重定向并显示登录页面。现在,当用户输入凭证并且AuthenticationBloc状态更改为authenticated时,什么也不会发生。它停留在登录屏幕上。
我甚至测试了在GoRouter的重定向中编写代码,但其行为相同。在GoRouter中,Bloc似乎没有更新。
我使用get_itAuthenticationBloc作为singlton注入,并在构建MaterialApp之前提供它。
即使使用context.push(x)来防止从小部件树中删除Listener,行为也保持不变。
有没有人对此有什么建议或建议?

更新

这是我使用get_it注入的AuthenticationBloc

locator.registerLazySingleton(() => AuthenticationBloc(
      authenticationRepository: locator<AuthenticationRepository>(),
      userRepository: locator<UserRepository>(),
      getUserDataUseCase: locator()));

在构建MaterialApp之前,我将singleton作为值提供:

BlocProvider.value(value: di.locator<AuthenticationBloc>()..add(AppStartup()))
qlckcl4x

qlckcl4x1#

这正是我们要走的路。错误出现在另一边。

相关问题