How to authenticate with phone number in flutter with firebase?

偶尔善良 提交于 2020-06-17 16:51:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!