问题
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
回答1:
i have found the solution from stackOverflow here,
final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) {
print('verified');
};
来源:https://stackoverflow.com/questions/57373903/how-to-authenticate-with-phone-number-in-flutter-with-firebase