Reputation: 3473
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";
}
}
Upvotes: 0
Views: 1462
Reputation: 969
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
Upvotes: 1
Reputation: 142
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
}
Upvotes: 2