I am trying to integrate authentication on Flutter using firebase_auth
.
However, whenever I call the verifyPhoneNumber("+256XXXXXXXXXX")
I get the error message A network error (such as timeout, interrupted connection or unreachable host) has occurred.
, that is from the PhoneVerificationFailed
callback. An for that reason cannot get the SMS.
I have tried;
Adding network permissions as seen below to my file (my internet connection works, as I am able to Google via the emulator)
<uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
Checking the validity of my API keys
I am totally confused as to why Flutter is unable to communicate with firebase. I have two questions.
My implimentatioin is as below;
import 'package:firebase_auth/firebase_auth.dart';
FirebaseAuth auth = FirebaseAuth.instance;
var message;
// fire this when Phone verification is completed
final PhoneVerificationCompleted verificationCompleted =
(AuthCredential phoneAuthCredential) {
auth.signInWithCredential(phoneAuthCredential);
message = 'Received phone auth credential: $phoneAuthCredential';
print(message);
};
// fire this when Phone verification fails
final PhoneVerificationFailed verificationFailed =
(AuthException authException) {
message =
'Phone verification failed. Code: ${authException.code}. Message: ${authException.message}';
print(message);
};
// fire this when SMS code is sent is sent.
final PhoneCodeSent codeSent =
(String verificationId, [int forceResendingToken]) async {
verificationId = verificationId;
print('Sent verification code');
};
// fire this when smsCode expires
final PhoneCodeAutoRetrievalTimeout codeAutoRetrievalTimeout =
(String verificationId) {
verificationId = verificationId;
print('Auto retrival time-out');
};
// verify phone number
verifyPhoneNumber(String phoneNumber) {
auth.verifyPhoneNumber(
phoneNumber: phoneNumber,
timeout: const Duration(seconds: 30),
verificationCompleted: verificationCompleted,
verificationFailed: verificationFailed,
codeSent: codeSent,
codeAutoRetrievalTimeout: codeAutoRetrievalTimeout);
print('Verification Initiated');
}
// sign in with phone.
signInWithPhoneNumber(String smsCode, String verificationId) async {
final AuthCredential credential = PhoneAuthProvider.getCredential(
verificationId: verificationId,
smsCode: smsCode,
);
final FirebaseUser user = (await auth.signInWithCredential(credential)).user;
final FirebaseUser currentUser = await auth.currentUser();
assert(user.uid == currentUser.uid);
if (user != null) {
message = 'Successfully signed in, uid: ' + user.uid;
} else {
message = 'Sign in failed';
}
}
In my case I solved allowing Outgoing Connections on the Xcode's Runner.xcworkspace
In my case, my VPN was causing the problem. Disabling the VPN and testing it again solved the error. I hope it helps.
if you are trying this out in a simulator, then this error may pop up as the internet of your simulator is not connected. To solve this you can run your app on a physical device and it will work!
Normally when the emulator is doing a lot of work on the thread, it tends to misbehave, such a losing internet connectivity
even if your PC is well connected.
My suggestion(Which worked for me) is you kill the emulator, and go to Android Studio AVD Manager
and Wipe Data
for that Emulator, then restart the Emulator, it worked for me.
Kindly find below the image screenshot
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With