我遇到了Firebase电话身份验证的问题,在该问题中,当我使用_auth.signInWithCredential()时,它不会从功能中消失并卡在那里。
这是我的代码:
Future<void> verifyPhoneNumber(String phoneNumber) async {
await _auth.verifyPhoneNumber(
phoneNumber: phoneNumber,
timeout: Duration(seconds: 60),
verificationCompleted: (AuthCredential credential) async {
print('Verification OTP completed');
AuthResult result = await _auth.signInWithCredential(credential);
FirebaseUser user = result.user;
if (user != null) {
print('User is not null!');
Navigator.pushReplacementNamed(context, 'home');
}
},
verificationFailed: (AuthException exception) async {
print('There was a problem sending the otp');
print(exception.code);
print(exception.message);
},
codeSent: (String verificationID, [int forceResendingToken]) {
createAlertDialog(context, widget.isFromGoogleAuth, verificationID);
},
codeAutoRetrievalTimeout: null);
}
接下来,当用户按下按钮时,我运行此命令:onTap: (pin) async {
otpText = pin.trim();
print('$otpText');
AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: verificationID, smsCode: otpText);
print('${verificationID}');
print('${credential.providerId}');
//everything until now has been printed, nothing is null
AuthResult authResult = await _auth
.signInWithCredential(credential)
.then((value) {
//this value is not printed
print('${value.user.phoneNumber}');
});
//The next line of code doesnt get run, and it doesnt print value.user.phone number
print('I HAVE REACHED HERE');
FirebaseUser user = authResult.user;
if (user != null) {
Navigator.pushReplacementNamed(context, 'home');
} else {}
},
最佳答案
问题在于,在测试用户中,我添加了我的电话号码,而不是使用在其中分配的OTP代码,而是使用一些发送到我的电话的旧OTP代码,这导致signInWithCredential引发错误。感谢您的帮助,我非常感谢