user10993979
user10993979

Reputation:

firebase_auth/invalid-phone-number in flutter?

enter image description here - I'm using phone authentication using firebase in flutter

I/flutter (27587): [firebase_auth/invalid-phone-number] The format of the phone number provided is incorrect. Please enter the phone number in a format that can be parsed into E.164 format. E.164 phone numbers are written in the format [+][country code][subscriber number including area code]. [ Invalid format. ]

I was tried sevaral ways but it did't work I was given my verify phone code for firebase given below

>  await auth.verifyPhoneNumber(
>           phoneNumber: phoneNumber,
>           verificationCompleted: verificationCompleted,
>           verificationFailed: verificationFailed,
>           codeSent: codeSent,
>           timeout: const Duration(seconds: 60),
>           codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);

my verify codesin ui section

 await authCalss.verifyPhonenumber(
                                      "+91 ${phonecontroller.text}",
                                      context,
                                      setData);

Upvotes: 3

Views: 10282

Answers (3)

sahil prajapati
sahil prajapati

Reputation: 544

I get same error while I did mobile phone authentication in Flutter web

error: the format of the phone number provided is incorrect. please enter the phone number in a format that can be parsed into e.164 format. e.164 phone numbers are written in the format [+][country code][subscriber number including area code].

I found authentication with a mobile number sample code here

await FirebaseAuth.instance.verifyPhoneNumber(
  phoneNumber: '+44 7123 123 456',
  verificationCompleted: (PhoneAuthCredential credential) {},
  verificationFailed: (FirebaseAuthException e) {},
  codeSent: (String verificationId, int? resendToken) {},
  codeAutoRetrievalTimeout: (String verificationId) {},
);

so, consider in Bharat(India) my mobile number is 9998887770, and my country code is +91

I need to +91 9998 887 770 format used while verifyPhoneNumber with Firebase authentication.

now in Textformfield I accept 10 digits only with no space allowed. like I will get 9998887770 format mobile number from my Textformfield.

The below code will provide formatted mobile number

  final phoneNumber = '+91 ${phone.substring(0, 4)} ${phone.substring(4, 7)} ${phone.substring(7)}'; // +91 **** *** *** format mobile number

My Own Code // work very well for me


  Future<void> verifyPhoneNumber({
    required String phone,
    required BuildContext context,
  }) async {
    try {
      final phoneNumber = '+91 ${phone.substring(0, 4)} ${phone.substring(4, 7)} ${phone.substring(7)}'; // +91 **** *** *** format mobile number
      
      await auth.verifyPhoneNumber(
        phoneNumber: phoneNumber ,
        verificationCompleted: (PhoneAuthCredential phoneAuthCredential) async {
          try {
            final UserCredential? credential = await auth.signInWithCredential(phoneAuthCredential);
            
          } on FirebaseAuthException catch (e) {
            Fluttertoast.showToast(msg: '${e.message}');
          } catch (e) {
            Fluttertoast.showToast(msg: '$e');
          }
        },
        codeAutoRetrievalTimeout: (String verificationId) {
          debugLog('codeAutoRetrievalTimeout : $verificationId');
        },
        codeSent: (String verificationId, int? forceResendingToken) {
         // MOVE TO OTP SCREEN 
          pushAndReplace(
            context: context,
            routeName: RouteName.otp,
          );
        },
        verificationFailed: (FirebaseAuthException error) {
        
          Fluttertoast.showToast(
            msg: '${error.message}',
          );
        },
      );
    } on FirebaseAuthException catch (e) {
     
      Fluttertoast.showToast(msg: '${e.code}');
    } catch (e) {
     
      Fluttertoast.showToast(msg: '$e');
    }
  }

Hope this will help you

Upvotes: 2

KurbanAli Masu
KurbanAli Masu

Reputation: 205

Your Phone Number must be formatted like this.

enter image description here

Upvotes: 2

ShinMyth
ShinMyth

Reputation: 644

There is no problem with the phone number +919999900000. It seems that the problem is in your configuration.

The configuration should look similar to like this:

enter image description here

Refer to this documentation: https://firebase.flutter.dev/docs/auth/phone

Upvotes: 1

Related Questions