'FIrebaseAuth verifyPhoneNumber crash in flutter IOS

I got this error when i call verifyPhoneNumber method:

My code works fine in android. Only in IOS it throws me this error. I checked a lot of issues on stackoverflow and github and have made sure that my GoogleService-Info.plist is in the right folder. I imported GoogleService-Info.plist through Xcode only and it is also added in CopyBundleResources section in Xcode-> Runner -> BuildPhases. Also a lot of places I saw that I need to add REVERSED_CLIENT_ID in URL Types in Xcode. Done this and also checked Info.plist that my REVERSED_CLIENT_ID is added correctly. I have done everything but still don't know what is causing the issue.

My code for mobile auth:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:[**package_name**]/functions/typedef.dart';
import 'package:[**package_name**]/helpers/log_helper.dart';

class MobileAuth{
  MobileAuth(
      this.mobileNumber,
      this.afterCodeSent,
      this.verificationCompleted,
      this.verificationFailed,
      this.autoTimeout,
      this.ifOtpFailed,
      this.afterOtpSend);

  final String mobileNumber;
  final AfterCodeSent afterCodeSent;
  final PhoneAuthVerificationCompleted verificationCompleted;
  final PhoneAuthVerificationFailed verificationFailed;
  final AuthFailedMessage autoTimeout;
  final AuthFailedMessage ifOtpFailed;
  final AfterOtpSend afterOtpSend;
  FirebaseAuth auth = FirebaseAuth.instance;


  /// to sent the code to the given mobile number
  void initialAuth() async{
    await auth.verifyPhoneNumber(
      phoneNumber: mobileNumber,
      verificationCompleted: (PhoneAuthCredential credential) {
        verificationCompleted(credential);
      },
      verificationFailed: (FirebaseAuthException e) {
        verificationFailed(e);
      },
      codeSent: (String verificationId, int? resendToken) {
         afterCodeSent(verificationId, resendToken ?? 0);
      },
      timeout: const Duration(seconds: 15),
      codeAutoRetrievalTimeout: (String verificationId) {
        autoTimeout(verificationId);
      },
    );
  }

  /// check otp by submitting to the auth api
  void verifyOtp(String smsCode, String verificationId) async{
    // Create a PhoneAuthCredential with the code
    PhoneAuthCredential credential = PhoneAuthProvider.credential(verificationId: verificationId, smsCode: smsCode);

    // Sign the user in (or link) with the credential
    try{
      UserCredential userCredential = await auth.signInWithCredential(credential);
      LogHelper.log("After sending OTP: ${userCredential.user?.uid}");
      afterOtpSend(userCredential);
    }catch(e){
      LogHelper.log("After sending OTP ${e.toString()}");
      ifOtpFailed(e.toString());
    }
  }

  void resendVerificationCode(int token) {
    auth.verifyPhoneNumber(
      forceResendingToken: token,
      phoneNumber: mobileNumber,
      verificationCompleted: (PhoneAuthCredential credential) {
        verificationCompleted(credential);
      },
      verificationFailed: (FirebaseAuthException e) {
        verificationFailed(e);
      },
      codeSent: (String verificationId, int? resendToken) {
        afterCodeSent(verificationId , resendToken ?? 0);
      },
      timeout: const Duration(seconds: 60),
      codeAutoRetrievalTimeout: (String verificationId) {
        autoTimeout(verificationId);
      },
    );
  }

}

And i call this in my statefull widget init method like this:

@override
void initState(){
String mobileNumber = "${widget.countryCode} ${widget.phoneNumber}";
    mobileAuth = MobileAuth(mobileNumber, afterCodeSent,
        verificationCompleted, verificationFailed, autoTimeout, ifOtpFailed, afterOtpSend);
sendOtpToPhone();
}

void sendOtpToPhone(){
    mobileAuth.initialAuth();
  }

The code works fine in android debug and release mode, so I only have this issue in IOS. Crashes when I call sendOtpToPhone(); -> mobileAuth.initialAuth(); -> auth.verifyPhoneNumber();

I have know for sure that auth.verifyPhoneNumber(); is only causing the error, because if I comment that segment of code then no crash is happening.

Also have tried flutter clean and i am using firebase_auth: 3.3.16 . Also tried it with 3.3.14.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source