如何在注销用户时重置 Flutter widget 树根部的 Multi Bloc 提供程序

问题描述 投票:0回答:1
 return HiveListener(
        box: Hive.box('SETTINGS'),
        builder: (bx) {
          return MultiBlocProvider(
            providers: [
              BlocProvider(create: (context) => ParentsBottomNavCubit()),
              BlocProvider(create: (context) => TeacherBottomNavCubit()),
              BlocProvider(create: (context) => AuthBloc()),
              BlocProvider(create: (context) => TeacherSubjectBloc()),
              BlocProvider(create: (context) => HomeWorkBloc()),
              BlocProvider(create: (context) => SubjectBloc(), lazy: true),
              BlocProvider(create: (context) => ProfileBloc()),
              BlocProvider(create: (context) => EventBloc(), lazy: true),
              BlocProvider(create: (context) => FeesBloc()),
              BlocProvider(create: (context) => TeacherAttendanceBloc()),
              BlocProvider(create: (context) => StudentBloc()),
              BlocProvider(create: (context) => FeedbackBloc()),
              BlocProvider(create: (context) => RoutineBloc()),
              BlocProvider(create: (context) => SummaryBloc()),
              BlocProvider(create: (context) => StudentAttendanceHistoryBloc()),
              BlocProvider(create: (context) => AwardsBloc()),
              BlocProvider(create: (context) => StudentBloc()),
              BlocProvider(create: (context) => WeeklyHomeworkBloc()),
              BlocProvider(create: (context) => StudentLeaderboardBloc()),
              BlocProvider(create: (context) => ChatBloc()),
            ],
            child: MaterialApp.router(
              title: 'Home Tracking',
              localizationsDelegates: const [
                AppLocalizations.delegate,
                GlobalMaterialLocalizations.delegate,
                GlobalWidgetsLocalizations.delegate,
                GlobalCupertinoLocalizations.delegate,
              ],
              locale: Locale(
                  Hive.box('SETTINGS').get('language', defaultValue: 'en')),
              supportedLocales: const [
                Locale('en'),
                Locale('ne'),
              ],
              debugShowCheckedModeBanner: false,
              routerConfig: AppRoutes.appRouter,
              theme: AppThemeData.lightModeThemeData(context),
            ),
          );
        });

注销用户不会重置状态值,注销用户时会出现错误。 我尝试通过将所有块发送到初始状态来重置它们,但它给出了错误。 请帮我解决这个问题。

flutter dart bloc logout provider
1个回答
0
投票
class AuthBloc extends Bloc<AuthEvent, AuthState> {
 AuthBloc() : super(AuthInitial()) {
   on<LoginEvent>((event, emit) async {
     try {
       emit(AuthLoading());
       await apiHandler(ApiConst.login, 'POST', body: {
         'email': emailController.text.trim(),
         'password': passwordController.text,
       }).then(
         (value) { 
           Hive.box('SETTINGS').put('token', value['data']);

           emit(AuthLoaded());
         },
       );
     } catch (e) {
       emit(AuthError(e.toString()));
     }
   });

   on<SendOTPEvent>((event, emit) async {
     try {
       emit(OTPSendLoading()); 
       await apiHandler(ApiConst.sendOtp, 'POST', body: {
         'email': event.email,
       }).then((value) {
         emit(OTPSendSuccess());
       });
     } catch (e) {
       emit(OTPSendFailure(e.toString()));
     }
   });

   on<ForgotPasswordOtpEvent>((event, emit) async {
     try {
       emit(OTPLoading());
       await apiHandler(
         ApiConst.validateOTP,
         'POST',
         body: {
           'email': event.email,
           'OTP': event.otp,
         },
       ).then((value) {
         Hive.box('SETTINGS').put('token', value['token']);
         emit(OTPSuccess());
       });
     } catch (e) {
       emit(OTPError(e.toString()));
     }
   });

   on<OtpCheckEvent>((event, emit) async {
     try {
       emit(OTPLoading()); 
       await apiHandler(
         ApiConst.validateOTP,
         'POST',
         body: {
           'email': event.email,
           'OTP': event.otp,
         },
       ).then((value) { 
         emit(OTPSuccess());
       });
     } catch (e) { 
       emit(OTPError(e.toString()));
     }
   });

   on<ResetPasswordEvent>((event, emit) async {
     try {
       emit(ResetPasswordLoading());
       await apiHandler(
         ApiConst.resetPassword,
         'POST',
         body: {
           'email': event.email,
           'OTP': event.otp,
           'password': event.password,
         },
       ).then((value) {
         Hive.box('SETTINGS').put('token', value['token']);
         emit(ResetPasswordSuccess());
       });
     } catch (e) {
       emit(ResetPasswordError(e.toString()));
     }
   });

   on<ResetAuthEvent>((event, emit) {
     emit(AuthInitial());
     emailController.clear();
     passwordController.clear();
   });
 }
 TextEditingController emailController = TextEditingController();
 TextEditingController passwordController = TextEditingController();

 TextEditingController forgotEmailController = TextEditingController();
 TextEditingController otpController = TextEditingController();
 TextEditingController resetPasswordController = TextEditingController();
 TextEditingController confirmPasswordController = TextEditingController();

 GlobalKey<FormState> forgotKey = GlobalKey<FormState>();
 GlobalKey<FormState> resetKey = GlobalKey<FormState>();
 GlobalKey<FormState> otpKey = GlobalKey<FormState>();
 GlobalKey<FormState> loginKey = GlobalKey<FormState>();

 @override
 Future<void> close() {
   emailController.dispose();
   passwordController.dispose();

   return super.close();
 }

 

}

这里是身份验证块的代码

© www.soinside.com 2019 - 2024. All rights reserved.