0

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

4
  • Have you tried this Commented Aug 6, 2019 at 11:24
  • I updated my code and got an exception Unhandled Exception: type '(FirebaseUser) => Null' is not a subtype of type '(AuthCredential) => void' in type cast Commented Aug 6, 2019 at 11:42
  • Can please also add pubspec.yaml Commented Aug 6, 2019 at 11:51
  • okay, I added pubspec.yaml Commented Aug 6, 2019 at 12:06

1 Answer 1

3

i have found the solution from stackOverflow here,

final PhoneVerificationCompleted verificationCompleted = (AuthCredential credential) {
   print('verified');
  };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.