BlocListener不响应状态更改

时间:2020-08-25 02:15:01

标签: flutter dart bloc flutter-bloc

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发出状态时,块会做出反应,但是当我们更改屏幕(登录过程)时,我会遇到问题。

感谢您对我的帮助

0 个答案:

没有答案