Flutter中FirebaseAuth无法发送短信验证码至+970xxxxxxxxx求助
Flutter FirebaseAuth 无法向+970开头手机号发送短信验证码问题
问题现象
使用Flutter开发时,FirebaseAuth无法向+970xxxxxxxxx格式的手机号发送短信验证码,日志提示等待短信超时。
代码实现
FirebaseAuth.instance.verifyPhoneNumber( phoneNumber: number, verificationCompleted: (PhoneAuthCredential credential) { }, verificationFailed: (FirebaseAuthException e) { setState(() { isLoading=false; }); print(e.message); if (e.code == 'invalid-phone-number') { print('The provided phone number is not valid.'); } }, codeSent: (String verificationId, int? resendToken) async { setState(() { isLoading=false; }); await Navigator.push(context, MaterialPageRoute(builder: (BuildContext context) { return verifyCode( verification: verificationId.toString(), registerModel: user, ); })); }, timeout: const Duration(seconds: 60), codeAutoRetrievalTimeout: (String verificationId) {}, );
依赖与配置
firebase_auth: ^4.1.1 firebase_core: ^2.1.1
Android构建配置:
minSdkVersion 19 targetSdkVersion flutter.targetSdkVersion multiDexEnabled true
错误日志
W/FirebaseAuth( 3798): [SmsRetrieverHelper] 等待短信超时。
排查与解决方案
- 校验手机号格式:+970为巴勒斯坦区号,确认号码完整且合规(巴勒斯坦本地手机号通常为8位,格式应为
+97059xxxxxxx这类),避免包含空格、特殊字符或位数错误。 - 检查Firebase项目配置:
- 登录Firebase控制台,确认已启用电话认证服务。
- 验证Android应用的SHA-1和SHA-256指纹已正确添加到项目设置中,这是Android端短信认证的必要条件。
- 查看Firebase控制台的用量配额,确认短信发送额度未耗尽,免费版配额有限,超出后会停止发送。
- 调整SmsRetriever配置:
- 确保AndroidManifest.xml中添加了SmsRetriever的广播接收器,用于自动获取验证码:
<receiver android:name="com.google.firebase.auth.api.internal.FirebaseAuthSMSCodeReceiver" android:exported="true"> <intent-filter> <action android:name="com.google.android.gms.auth.api.phone.SMS_RETRIEVED"/> </intent-filter> </receiver> - 若不需要自动检索验证码,可专注于手动输入流程,避免自动检索超时干扰。
- 确保AndroidManifest.xml中添加了SmsRetriever的广播接收器,用于自动获取验证码:
- 升级依赖版本:当前使用的firebase_auth和firebase_core版本较旧,存在已知的地区兼容或bug问题,建议升级到最新稳定版,例如:
firebase_auth: ^4.16.0 firebase_core: ^2.24.2 - 完善错误捕获:在
verificationFailed回调中打印完整的错误信息(如e.code、e.details),排查是否存在配额不足、服务未启用等隐藏错误,不要仅打印message。 - 检查网络环境:确保设备能正常访问Firebase服务,避免防火墙拦截Firebase的短信服务请求。
内容的提问来源于stack exchange,提问作者Abdulsalam Sawalha
相关产品推荐
相关产品推荐

