0

I am trying to fetch the verificationid from APIClient class to login screen.

**In Login.dart**
       FetchVerificationID() {
              ApiProviderObj.FetchFirebaseVeriID().then((value) {
                print(value); // This always get null
              });
            }

In APIPROVIDER.dart class

Future<String> FetchFirebaseVeriID() async {
    final PhoneCodeSent smsOTPSent = (String verId, [int forceCodeResend]) {
      verificationId = verId;
      return verId; // This is what i am expecting the return value. It take some time to reach here but the method return value before reaching here
    };
    try {
      await _auth.verifyPhoneNumber(
          phoneNumber: '+91 93287 46333', // PHONE NUMBER TO SEND OTP
          codeAutoRetrievalTimeout: (String verId) {
            //Starts the phone number verification process for the given phone number.
            //Either sends an SMS with a 6 digit code to the phone number specified, or sign's the user in and [verificationCompleted] is called.
            verificationId = verId;
          },
          codeSent:
          smsOTPSent, // WHEN CODE SENT THEN WE OPEN DIALOG TO ENTER OTP.
          timeout: const Duration(seconds: 20),
          verificationCompleted: (AuthCredential phoneAuthCredential) {
            print('phoneAuthCredential => ${phoneAuthCredential}');
            return verId;
          },
          verificationFailed: (AuthException exceptio) {
           return "Error";
          });
    } catch (e) {
      return "Error";
    }
  }
2
  • Looks like you return nothing in try {} block. Commented Feb 26, 2020 at 19:52
  • @AlexMyznikov thank you for the comment. I wanted to return Verificationid but in try block i dont have that yet. Its like i have sync menthod inside other sync method. This is reason i am stuck on this Commented Feb 26, 2020 at 19:57

2 Answers 2

2

You should have the return statement at the end of the method.

I don't really know what is the cause of this, but I had an issue like you with a FutureBuilder.

In the async method I have the return values, but the return value was null.

Future<String> FetchFirebaseVeriID() async {

    String returnVar; //Create return variable

    final PhoneCodeSent smsOTPSent = (String verId, [int forceCodeResend]) {
      verificationId = verId;
      return verId;
    };
    try {
      await _auth.verifyPhoneNumber(
          phoneNumber: '+91 93287 46333', // PHONE NUMBER TO SEND OTP
          codeAutoRetrievalTimeout: (String verId) {
            //Starts the phone number verification process for the given phone number.
            //Either sends an SMS with a 6 digit code to the phone number specified, or sign's the user in and [verificationCompleted] is called.
            verificationId = verId;
          },
          codeSent:
          smsOTPSent, // WHEN CODE SENT THEN WE OPEN DIALOG TO ENTER OTP.
          timeout: const Duration(seconds: 20),
          verificationCompleted: (AuthCredential phoneAuthCredential) {
            print('phoneAuthCredential => ${phoneAuthCredential}');
            returnVar = verId; //Set the return value
          },
          verificationFailed: (AuthException exceptio) {
           returnVar = "Error"; //Set the return value
          });
    } catch (e) {
      returnVar = "Error"; //Set the return value
    }

    return returnVar; //And return the value

  }
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the answer. I did tried that but its not working . becase returnVar = null Where as it take some time to reach to final PhoneCodeSent smsOTPSent = (String verId, [int forceCodeResend]) { verificationId = verId; return verId; }
Verify that you have await or .then in all your async calls. If the value set to the smsOTPSent is async do PhoneCodeSent smsOTPSent = await (String verId, [int forceCodeResend]) { verificationId = verId; return verId; }
I added debugger. Problem is it does not take time to go inside the _auth.verifyphonenumber or smsOTPSent method. it just moves outside and return the value and after sometime by breakpoint move inside those method. so it is taking time to goes inside but by the time it just return the value
You can set a timeout to the future to wait more time.. api.dart.dev/stable/2.7.1/dart-async/Future/timeout.html
1

I think you can try to use the Completer class to complete your Future when verificationCompleted gets called and verId is available.

Like so:

Future<String> FetchFirebaseVeriID() async {

    // ...

    final completer = new Completer();

    try {
      await _auth.verifyPhoneNumber(

          // ...

          verificationCompleted: (AuthCredential phoneAuthCredential) {
            print('phoneAuthCredential => ${phoneAuthCredential}');
            completer.complete(verId);
          },
          verificationFailed: (AuthException exceptio) {
           completer.completeError("Error");
          });
    } catch (e) {
      completer.completeError("Error");
    }

    return completer.future;
}

https://api.dart.dev/stable/2.7.1/dart-async/Completer-class.html

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.