BlocListener不响应状态更改

问题描述

BlockListener应该在成功登录或成功注册后打开欢迎窗口。在这两种情况下,BlocListener均不响应。初始化方法如下:

    runApp(
    MultiRepositoryProvider(
      providers: [
        RepositoryProvider<UserRepository>(
          create: (context) => UserRepository(storage: storage)
        ),RepositoryProvider<SettingsRepository>(
          create: (BuildContext context) => SettingsRepository(storage: storage)
        ),RepositoryProvider<SMSRepository>(
          create: (BuildContext context) => SMSRepository(storage: storage)
        ),],child: MultiBlocProvider(
        providers: [
          BlocProvider<AuthentificationBloc>(
            create: (BuildContext context) {
              return AuthentificationBloc(userRepository: context.repository<UserRepository>());
            }
          ),BlocProvider<SUSPECTBloc>(
              create: (BuildContext context) => SUSPECTBloc()
          )
        ],child: BlocProvider<SigninBloc>(
              create: (BuildContext context) => SigninBloc(
                userRepository: RepositoryProvider.of<UserRepository>(context),SUSPECTBloc: BlocProvider.of<SUSPECTBloc>(context),authentificationBloc: BlocProvider.of<AuthentificationBloc>(context)),child: App()
          )
      )
    )
  );
}

class App extends StatelessWidget {

  @override
  Widget build(BuildContext context) {

    return MaterialApp(
        onGenerateRoute: RouteGenerator.generateRoute,debugShowCheckedModeBanner: false,theme: DeliveroGnTheme.of(context),home: BlocListener<SUSPECTBloc,SUSPECTException>(
        listener: (context,state) =>_shouldHandleException(context,state),child: BlocListener<AuthentificationBloc,AuthenticationState>(
          listener: (context,state) {

              if (state is AuthenticationSuccess) {
                 Navigator.of(context).pushReplacementNamed('/DataRecovery');
              }else if (state is AuthenticationFailure) {
                Navigator.of(context).pushReplacementNamed('/Login');
              }else if(state is AuthenticationInProgress) {
                Navigator.pushReplacement(context,MaterialPageRoute(builder: (_) {
                  return CircularLoadingWidget();
                }));
              }
            },child: SplashScreen()
          )
        )
      );
  }

AuthentificationBloc

    class AuthentificationBloc
    extends Bloc<AuthenticationEvent,AuthenticationState> {
    final UserRepository userRepository;

    AuthentificationBloc({@required this.userRepository})
        : assert(userRepository != null),super(AuthenticationInitial());

  @override
  AuthenticationState get initialState => AuthenticationInitial();

  @override
  Stream<AuthenticationState> mapEventToState(
    AuthenticationEvent event,) async* {

    yield StartAuthentication();

    if (event is AuthenticationStarted) {
      final bool hasCredentials = userRepository.hasCredentials();

      if (hasCredentials) {
        yield AuthenticationSuccess();
      } else {
        yield AuthenticationFailure();
      }
    }

    if (event is AuthenticationLoggedOut) {
      yield AuthenticationInProgress();
      await userRepository.logout();
      yield AuthenticationFailure();
    }

  }
}

在连接后调用AuthentificationBloc的SignBloc块:

   class SigninBloc
    extends Bloc<SigninEvent,BlocResponse<bool>> {

    final UserRepository userRepository;
    final SUSPECTBloc SUSPECTBloc;
    AuthentificationBloc authentificationBloc;

    SigninBloc({@required this.userRepository,@required this.SUSPECTBloc,this.authentificationBloc})
        : assert(userRepository != null),super(BlocResponse.initial());

  @override
  BlocResponse<bool> get initialState => BlocResponse.initial();

  @override
  Stream<BlocResponse<bool>> mapEventToState(
    SigninEvent event,) async* {

    try{

      if (event is LoggedIn) {

        yield BlocResponse.loading('Loading');

        print(event.phoneNumber);

        final bool authenticate = await userRepository.authenticate(
                                          event.phoneNumber,event.password
                                        );
        if(authenticate){

          authentificationBloc.add(AuthenticationStarted());
          
          yield BlocResponse.completed(authenticate);
        }
        else{
          yield BlocResponse.error("incorrect password");
        }
      }

    }catch (e,tracklist){

      if(e is SUSPECTException){
        SUSPECTBloc.emit(e);
      }else{
        print(tracklist);
      }


      yield BlocResponse.error(e.toString());
      
    }

    
  }
}

我指定当SplashScreen发出状态时,块会做出反应,但是当我们更改屏幕(登录过程)时,我会遇到问题。

感谢您对我的帮助

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)