如果我向手机发送短信,则短信用户同意 API 会收到良好的短信。
如果我通过 FirebaseAuth 向我的手机发送短信,则短信会成功发送到我的设备。
但是当我使用 Firevase 发送短信并尝试使用短信用户同意 API 捕获它时,我会崩溃
02-05 08:47:05.119 packege 26223 - E AndroidRuntime FATAL EXCEPTION: main
02-05 08:47:05.119 packege 26223 - E AndroidRuntime Process: packege , PID: 26223
02-05 08:47:05.119 packege 26223 - E AndroidRuntime java.lang.RuntimeException: Error receiving broadcast Intent { act=com.google.android.gms.auth.api.phone.SMS_RETRIEVED flg=0x200010 pkg=packege (has extras) } in com.google.firebase.auth.api.internal.zzgn@19feaf8
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1656)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at android.os.Handler.handleCallback(Handler.java:883)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at android.os.Handler.dispatchMessage(Handler.java:100)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at android.os.Looper.loop(Looper.java:237)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at android.app.ActivityThread.main(ActivityThread.java:8016)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at java.lang.reflect.Method.invoke(Native Method)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1076)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at java.util.regex.Matcher.reset(Matcher.java:280)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at java.util.regex.Matcher.<init>(Matcher.java:186)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at java.util.regex.Pattern.matcher(Pattern.java:1034)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at com.google.firebase.auth.api.internal.zzgj.zzc(com.google.firebase:firebase-auth@@20.0.0:112)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at com.google.firebase.auth.api.internal.zzgn.onReceive(com.google.firebase:firebase-auth@@20.0.0:13)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0$LoadedApk$ReceiverDispatcher$Args(LoadedApk.java:1646)
02-05 08:47:05.119 packege 26223 - E AndroidRuntime ... 8 more
我尝试在 Activity 开始时、Firebase 发送 SMS 之后、Firebase 发送之前、Firebase 发送延迟后运行 SMS User Consent API,但总是崩溃:(
再一次,我单独重复一遍,两者都工作得很好,但在一起它们就会崩溃!
源代码https://github.com/StenleyDuFoster/ExampleFirebasePhoneAuthWithOneTapVerificationApi
它们无法一起工作,因为同一 api 在某些时候都会崩溃并给出错误。唯一的解决方案是删除 smsretriever 或更改为不包括 firebase 的另一个源。
一起可以工作,,只是放 超时:const Duration(秒:0),在 verifyPhoneNumber 函数中。
例如
auth.verifyPhoneNumber(
phoneNumber: "+91${controllerforotp.phonenumberController.text}",
verificationCompleted: (PhoneAuthCredential phoneauth) {
print('succeed');
setState(() {
loading = false;
});
VerifyOTP.otp = phoneauth.smsCode ?? "";
},
verificationFailed: (e) {
print(e);
print('failed');
setState(() {
loading = false;
});
toastMessage("Otp not sent");
},
codeSent: (String verificationId, int? token) async {
setState(() {
loading = false;
});
navigationService.push(
context: context,
widget: VerifyOTP(
phone: controllerforotp.phonenumberController.text,
verificationId: verificationId,
resendtoken: token,
));
},
timeout: const Duration(seconds: 0),
codeAutoRetrievalTimeout: (e) {
setState(() {
loading = false;
});
});