【发布时间】:2021-02-06 10:43:36
【问题描述】:
我正在尝试使用手机对用户进行身份验证,当我使用 flutter_bloc 和 Repository 时,我所采用的方法是:
- 将带有电话号码的事件发送到 Bloc(OK)。
- 拨打
verifyPhoneNumber,输入电话号码(OK)。 - 返回从
codeAutoRetrievalTimeout或codeSent回调接收到的verificationId。 - 将其从
BlocListener发送到 UI 并带有状态。 - 更新 UI 以输入验证码。
- 将 smsCode 和 verifyId 发送到另一种方法以将手机 AuthCredential 链接到用户。
我的方法在调用回调之前返回,因为 verifyPhoneNumber 首先完成,所以它返回一个空字符串。
我做错了什么? 一如既往,非常感谢您的帮助。
Future<String> verifyPhoneNumber(String phoneNumber) async {
print('verifyPhoneNumber() started');
String verifyId;
await _firebaseAuth
.verifyPhoneNumber(
phoneNumber: phoneNumber,
timeout: Duration(minutes: 0),
//error: Undefined class 'PhoneAuthCredential'.
// verificationCompleted: (PhoneAuthCredential credential) async {
verificationCompleted: (AuthCredential credential) {
},
//error: Undefined class 'FirebaseAuthException'.
// verificationFailed: (FirebaseAuthException e) {
verificationFailed: (AuthException e) {
if (e.code == 'invalid-phone-number') {
print(
'verifyPhoneNumber() -> verificationFailed -> The provided phone number is not valid.');
} else {
print(
'verifyPhoneNumber() -> verificationFailed :${e.message}');
}
// Handle other errors
},
codeAutoRetrievalTimeout: (String verificationId) {
// Auto-resolution timed out...
// verifyId = verificationId;
print(
'verifyPhoneNumber() -> codeAutoRetrievalTimeout callback called');
},
//error: The argument type 'Null Function(String, int)' can't be assigned to the parameter type 'void Function(String, [int])'.
// codeSent: (String verificationId, int resendToken) {
codeSent: (String verificationId, [int resendToken]) {
verifyId = verificationId;
print(
'verifyPhoneNumber() -> codeSent callback called : $verifyId'); // correct
return verifyId;
}
)
.then((value) {
print('verifyId is $verifyId'); // null
// return verifyId;
}).whenComplete(() => print('Complete'));
print('verifyPhoneNumber() ->returning verifyId: $verifyId');
return verifyId; // without it method doesn't return, with it returns null
}
【问题讨论】: