dart 此代码有什么问题,因为它没有侦听状态更改

ehxuflar  于 2023-03-27  发布在  其他
关注(0)|答案(1)|浏览(131)

我正在使用flutter_bloc库和HydratedBloc来创建Auth bloc。这里的问题是BlocListener没有监听状态更改,
所以请让我知道我在这段代码中遗漏了什么?

  • (我认为初始状态将从函数'fromJson'返回,但它不会发出新状态)* 以下是我尝试做的事情
class App extends StatelessWidget {
          const App({super.key});
        
          @override
          Widget build(BuildContext context) {
            return MultiBlocProvider(
                providers: [
                  BlocProvider(create: (context) => AuthBloc()),
                ],
                child: MaterialApp(
                  debugShowCheckedModeBanner: false,
                  theme: Themes.light,
                  darkTheme: Themes.dark,
                  themeMode: ThemeMode.dark,
                  title: AppContants.appName,
                  navigatorKey: NavigationService.navigatorKey,
                  onGenerateRoute: Routes.navigateTo,
                  builder: (BuildContext context, Widget? child) {
                    child = EasyLoading.init()(context, child);
        
                    return BlocListener<AuthBloc, AuthState>(
                        listener: (context, AuthState state) {
                          //bloc listener is used to make a actions instead of builds
                          //bloc bulder is used to return a widget
                          print('state is $state');
                          if (state is AuthAuthenticatedState) {
                            // NavigationService.navigateAndRemoveUntil(Confirm.route);
        
                            NavigationService.navigateAndRemoveUntil(
                              Dashboard.route,
                            );
                          } else {
                            NavigationService.navigateAndRemoveUntil(Login.route);
                          }
                        },
                        child: child);
                  },
                ));
          }
        }
        
        
    
    class AuthBloc extends HydratedBloc<AuthEvent, AuthState> {
          AuthBloc() : super(AuthInitial()) {
            on<AuthAuthenticatingEvent>(authAuthenticating);
            on<AuthUnAuthenticatingEvent>(authUnAuthenticating);
          }
        
          authAuthenticating(event, emit) async {
            await Future.delayed(const Duration(seconds: 1));
            emit(const AuthAuthenticatedState(''));
          }
        
          authUnAuthenticating(event, emit) {}
        
          @override
          AuthState? fromJson(Map<String, dynamic> json) {
            String? token = json['token'];
            if (token != null && token.isNotEmpty) {
              return AuthAuthenticatedState(token);
            }
        
            return AuthUnAuthenticatedState();
          }
        
          @override
          Map<String, dynamic>? toJson(AuthState state) {
            if (state is AuthAuthenticatedState) {
              return {'token': state.token};
            }
            return {};
          }
        }

先谢了

8zzbczxx

8zzbczxx1#

您必须将AuthAuthenticatingEvent添加到AuthenticationBloc,例如使用

BlocProvider(create: (context) => AuthBloc()..add(AuthAuthenticatingEvent()))

否则,您的Bloc将不会调用authAuthenticating(event, emit)
或者你已经在代码中的某个地方调用了这个函数?如果是这样,你应该把它加到你的问题中。

相关问题