Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter Firebase Auth: A network error (such as timeout, interrupted connection or unreachable host) has occurred

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;

  1. 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"/>

  2. 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.

  1. How can I eliminate this error?
  2. What other cases might cause this error besides a lacking internet connection?

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';
  }
}
like image 936
M4X_ Avatar asked Feb 22 '20 12:02

M4X_


4 Answers

In my case I solved allowing Outgoing Connections on the Xcode's Runner.xcworkspace

enter image description here

like image 141
Fran Fox Avatar answered Nov 15 '22 09:11

Fran Fox


In my case, my VPN was causing the problem. Disabling the VPN and testing it again solved the error. I hope it helps.

like image 27
NBM Avatar answered Nov 15 '22 10:11

NBM


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!

like image 36
Abhiraj Ghosh Avatar answered Nov 15 '22 11:11

Abhiraj Ghosh


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.The circled in red are the key points to click Kindly find below the image screenshot

like image 21
Jacob Okello Okomo Avatar answered Nov 15 '22 10:11

Jacob Okello Okomo