Flutter Android模拟器内部错误:FirebaseAuth登录异常捕获失效
Flutter Firebase邮箱密码登录异常排查及捕获问题
问题描述
我使用VS Code开发Flutter应用,在Android 13版本的Pixel 3模拟器上执行Firebase邮箱密码登录时遇到问题。错误日志显示INVALID_LOGIN_CREDENTIALS内部错误,但代码中的FirebaseAuthException捕获块无法打印e.code,即使捕获所有异常也无效,尝试打印“User-not found”语句同样失败。
错误日志
I/FirebaseAuth(26473): Logging in as xyz@xyz.xyzw with empty reCAPTCHA token D/EGL_emulation(26473): app_time_stats: avg=196.98ms min=3.58ms max=487.15ms count=5 W/System (26473): Ignoring header X-Firebase-Locale because its value was null. D/TrafficStats(26473): tagSocket(141) with statsTag=0xffffffff, statsUid=-1 E/RecaptchaCallWrapper(26473): Initial task failed for action RecaptchaAction(action=signInWithPassword)with exception - An internal error has occurred. [ INVALID_LOGIN_CREDENTIALS ] I/flutter (26473): INVALID_LOGIN_CREDENTIALS
相关代码
try { final userCredential = await FirebaseAuth.instance .signInWithEmailAndPassword( email: email, password: password); print(userCredential); } on FirebaseAuthException catch (e) { print(e.code); }
解决方案建议
- 升级
firebase_auth依赖到最新稳定版,旧版本可能存在异常捕获逻辑的bug - 扩展异常捕获范围,覆盖所有可能的异常类型,确保不会遗漏:
try { final userCredential = await FirebaseAuth.instance .signInWithEmailAndPassword(email: email, password: password); print(userCredential); } on FirebaseAuthException catch (e) { print('FirebaseAuth异常:${e.code} - ${e.message}'); } on Exception catch (e) { print('通用异常:$e'); } catch (e) { print('未知错误:$e'); } - 检查模拟器网络连接状态,确保能正常访问Firebase服务,日志中“empty reCAPTCHA token”可能是网络问题导致验证失败
- 确认Firebase控制台已启用邮箱/密码登录方式,同时检查测试账号的邮箱格式是否正确、密码是否匹配
- 清理模拟器缓存,重启VS Code和模拟器后重新构建应用
内容的提问来源于stack exchange,提问作者Rishabh Agarwal
相关产品推荐
相关产品推荐

