如何在带有Firebase的Flutter中使用电话号码进行身份验证? [英] How to authenticate with phone number in flutter with firebase?
问题描述
我不熟悉
,并且使用firebase验证电话号码,但是我无法进行验证。
我浏览了它,但是找不到任何令人满意的解决方案。
我从StackOverflow实现了一些代码,但是发生了异常'Unhandled Exception:type' (FirebaseUser)=> Null'不是类型强制转换类型'(AuthCredential)=> void'的子类型,并且无法捕获异常。
signIn()async {
AuthCredential credential = PhoneAuthProvider.getCredential(
VerificationId:VerificationId,
smsCode:smsCode
) ;
等待firebaseAuth.signInWithCredential(凭据).then((用户){
Navigator.of(context).pushReplacementNamed('/ homepage');
print('已成功登录电话号码:user-> $ user');
})。catchError((e){
print(e);
});
}
它将向您发送给指定电话的OTP
Future< void>。 verifyPhone()async {
final PhoneCodeAutoRetrievalTimeout autoRetrieval =(String verId){
this.verificationId = verId;
};
final PhoneCodeSent smsCodeSent =(String verId,[int forceCodeResend]){
this.verificationId = verId;
smsCodeDialog(context).then((value){
print( Signed in);
});
};
最终的PhoneVerificationCompleted VerificationCompleted =(FirebaseUser用户){
print(‘verified’);
}作为PhoneVerificationCompleted;
最终的PhoneVerificationFailed verfifailed =(AuthException异常){
print( $ {exception.message});
};
等待firebaseAuth.verifyPhoneNumber(
电话号码:this.phoneNo,
codeAutoRetrievalTimeout:autoRetrieval,
codeSent:smsCodeSent,
timeout:const Duration(seconds: 10),
验证完成:VerificationCompleted,
验证失败:验证失败
);
}
pubspec.yaml
< pre $ = lang-dart prettyprint-override>
名称:babilok
说明:一个新的Flutter项目。
版本:1.0.0 + 1
环境:
sdk:> = 2.1.0< 3.0.0
依赖项:
抖动:
sdk:抖动
cupertino_icons:^ 0.1.2
page_transition:
firebase_auth:
dev_dependencies:
flutter_test:
sdk:flutter
use-material-design:true
#添加资产到您的应用程序中,添加一个资产部分,例如:
资产:
-asset / login_wheel.png
-资产/login_wheel1.png
-资产/forward_arrow.png
-资产/google_ads.png
-资产/wheel.png
-资产/red_cross.png
-资产/welcome_logo.png
-资产/money_bag.png
-资产/user.png
-资产/money_bag.png
-资产/up_gift_wheel.png
-资产/cross.png
-资产/图标/ facebook_logo1。 png
-资产/图标/twitter_logo1.png
-资产/图标/instagram_logo1.png
我从stackOverflow 此处,
最终PhoneVerificationCompleted VerificationCompleted =(AuthCredential凭据){
print('verified');
};
I'm new to flutter, and using firebase to authenticate with phone number but I m unable to authenticate. I browsed for it but couldn't get any satisfying solution.
I implemented some code from the StackOverflow but there is occurring Exception 'Unhandled Exception: type '(FirebaseUser) => Null' is not a subtype of type '(AuthCredential) => void' in type cast' and couldn't catch the Exception.
signIn()async{
AuthCredential credential= PhoneAuthProvider.getCredential(
verificationId: verificationId,
smsCode: smsCode
);
await firebaseAuth.signInWithCredential(credential).then((user){
Navigator.of(context).pushReplacementNamed('/homepage');
print('signed in with phone number successful: user -> $user');
}).catchError((e){
print(e);
});
}
It will send you OTP to given phone number.
Future<void> verifyPhone()async{
final PhoneCodeAutoRetrievalTimeout autoRetrieval=(String verId){
this.verificationId=verId;
};
final PhoneCodeSent smsCodeSent=(String verId, [int forceCodeResend]){
this.verificationId=verId;
smsCodeDialog(context).then((value){
print("Signed in");
});
};
final PhoneVerificationCompleted verificationCompleted = (FirebaseUser user) {
print('verified');
} as PhoneVerificationCompleted;
final PhoneVerificationFailed verfifailed=(AuthException exception){
print("${exception.message}");
};
await firebaseAuth.verifyPhoneNumber(
phoneNumber: this.phoneNo,
codeAutoRetrievalTimeout: autoRetrieval,
codeSent: smsCodeSent,
timeout: const Duration(seconds: 10),
verificationCompleted: verificationCompleted,
verificationFailed: verfifailed
);
}
pubspec.yaml
name: babilok
description: A new Flutter project.
version: 1.0.0+1
environment:
sdk: ">=2.1.0 <3.0.0"
dependencies:
flutter:
sdk: flutter
cupertino_icons: ^0.1.2
page_transition:
firebase_auth:
dev_dependencies:
flutter_test:
sdk: flutter
uses-material-design: true
# To add assets to your application, add an assets section, like this:
assets:
- assets/login_wheel.png
- assets/login_wheel1.png
- assets/forward_arrow.png
- assets/google_ads.png
- assets/wheel.png
- assets/red_cross.png
- assets/welcome_logo.png
- assets/money_bag.png
- assets/user.png
- assets/money_bag.png
- assets/up_gift_wheel.png
- assets/cross.png
- assets/icons/facebook_logo1.png
- assets/icons/twitter_logo1.png
- assets/icons/instagram_logo1.png
i have found the solution from stackOverflow here,
final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) {
print('verified');
};
这篇关于如何在带有Firebase的Flutter中使用电话号码进行身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!