Flutter Bloc登录无响应及注册报错问题求助
Flutter Bloc集成问题排查与修复
问题描述
- 点击登录按钮无任何响应,控制台输出报错信息
- 点击注册按钮时,控制台出现报错信息
项目仓库地址:git@github.com:GANONTHA/mynotebook.git
相关代码
AuthBloc 文件
class AuthBloc extends Bloc<AuthEvent, AuthState> { AuthBloc(AuthProvider provider) : super(const AuthStateUninitialized()) { // 发送邮件验证 on<AuthEventSendEmailVerification>((event, emit) async { await provider.sendEmailVerification(); emit(state); }); // 注册 on<AuthEventRegister>((event, emit) async { final email = event.email; final password = event.password; try { await provider.createUser( email: email, password: password, ); await provider.sendEmailVerification(); emit(const AuthStateNeedsVerification()); } on Exception catch (e) { emit(AuthStateRegistering(e)); } }); // 初始化 on<AuthEventInitialize>((event, emit) async { await provider.initialize(); final user = provider.currentUser; if (user == null) { emit( const AuthStateLoggedOut( exception: null, isLoading: false, ), ); } else if (!user.isEmailVerified) { emit(const AuthStateNeedsVerification()); } else { emits(AuthStateLoggedIn(user)); // 此处存在拼写错误 } }); // 登录 on<AuthEventLogIn>((event, emit) async { emit(const AuthStateLoggedOut( exception: null, isLoading: true, )); final email = event.email; final password = event.password; try { final user = await provider.logIn( email: email, password: password, ); if (!user.isEmailVerified) { emit( const AuthStateLoggedOut( exception: null, isLoading: false, ), ); emit(const AuthStateNeedsVerification()); } else { emit( const AuthStateLoggedOut( exception: null, isLoading: false, ), ); emit(AuthStateLoggedIn(user)); } emit(AuthStateLoggedIn(user)); // 此处重复emit状态 } on Exception catch (e) { emit(AuthStateLoggedOut( exception: e, isLoading: false, )); } }); // 登出 on<AuthEventLogOut>((event, emit) async { try { await provider.logOut(); emit(const AuthStateLoggedOut( exception: null, isLoading: false, )); } on Exception catch (e) { emit(AuthStateLoggedOut( exception: e, isLoading: false, )); } }); } }
AuthEvent 文件
import 'package:flutter/foundation.dart' show immutable; // 定义事件类及其构造函数 @immutable abstract class AuthEvent { const AuthEvent(); } class AuthEventInitialize extends AuthEvent { const AuthEventInitialize(); } class AuthEventSendEmailVerification extends AuthEvent { const AuthEventSendEmailVerification(); } class AuthEventLogIn extends AuthEvent { final String email; final String password; const AuthEventLogIn(this.email, this.password); } class AuthEventRegister extends AuthEvent { final String email; final String password; const AuthEventRegister(this.email, this.password); } class AuthEventShouldRegister extends AuthEvent { const AuthEventShouldRegister(); } class AuthEventLogOut extends AuthEvent { const AuthEventLogOut(); }
问题修复方案
登录无响应问题修复:
在AuthBloc的AuthEventInitialize事件处理逻辑中,将emits(AuthStateLoggedIn(user))改为emit(AuthStateLoggedIn(user))——此处是拼写错误,多了一个s,会导致Bloc初始化逻辑中断,后续登录事件无法正常触发。登录事件冗余状态处理:
登录事件末尾重复emit(AuthStateLoggedIn(user))会导致状态重复发送,可删除该行,避免不必要的状态更新。注册报错排查方向:
- 检查
AuthProvider的createUser方法,确保所有可能的错误都被捕获并转换为Exception类型抛出; - 确认注册按钮触发的是
AuthEventRegister事件,而非其他事件。
- 检查
内容的提问来源于stack exchange,提问作者Bruno Ganontha Payang
相关产品推荐
相关产品推荐

