【发布时间】:2020-07-22 17:25:20
【问题描述】:
这可能是重复的,但任何其他线程都没有为我提供正确的答案。
有关于 android 原生语言的答案,但没有关于 Flutter(dart) 的答案。
我有以下有效的方法,但如果我想向用户电话号码重新发送 OTP,我该怎么做?只是一个简单的示例代码可能会有所帮助。
Future signInWithPhone(String phone, BuildContext context) async {
// This triggers if verification passes
final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) async {
Navigator.of(context).pop();
AuthResult result = await _auth.signInWithCredential(credential);
FirebaseUser user = result.user;
if(user != null){
Navigator.push(context, MaterialPageRoute(
builder: (context) => HomeScreen(user: user,)
));
}else{
showDialog(
context: context,
builder: (BuildContext context) {
return AlertDialog(
title: Text("Alert Dialog"),
content: Text('Error'),
);
}
);
}
};
// This triggers if verification fails
PhoneVerificationFailed verificationFailed = (AuthException exception) {
toast(exception.message, 'long');
};
// This is to send code to the user But i dont know how to resend
final PhoneCodeSent codeSent = (String verificationId, [int forceResendingToken]) {
var route = MaterialPageRoute(
builder: (BuildContext context) => LoginPhoneVerify(verificationId, phone)
);
Navigator.of(context).push(route);
};
// The main function
await _auth.verifyPhoneNumber(
phoneNumber: phone,
timeout: Duration(seconds: 0),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: null
);
}
我在以下线程中找到了适用于 android 的内容:
https://stackoverflow.com/a/44688838/10114772
private void resendVerificationCode(String phoneNumber, PhoneAuthProvider.ForceResendingToken token) {
PhoneAuthProvider.getInstance().verifyPhoneNumber(
phoneNumber, // Phone number to verify
60, // Timeout duration
TimeUnit.SECONDS, // Unit of timeout
this, // Activity (for callback binding)
mCallbacks, // OnVerificationStateChangedCallbacks
token); // ForceResendingToken from callbacks
}
但那不是颤抖的,请有人看看!
【问题讨论】:
-
Flutter 的 Firebase API 与 Android 没有太大区别。如果您看到一个 Android 解决方案,如果您只是在 API 文档中搜索它,那么很可能有一个 Flutter 等价物。
-
你可能是对的,但我想不通尝试了很多线程和网站
-
好吧,如果您编辑问题以包含有关 Android 解决方案的一些信息,那么其他人可能很容易找到 Flutter 等效项。
-
@DougStevenson 我已经包含了 android 解决方案
标签: flutter dart firebase-authentication