0

I keep receiving an error when running on flutter:

FirebaseAuthException ([firebase_auth/unknown] An internal error has occurred. [ Failed to connect to /10.0.2.2:9099)

I'm using a physical Android device, connected via cable, and I try to access the firebase emulator on the computer.

My flutter code:

String ip = kIsWeb ? 'localhost' : 'localhost'; // 10.0.2.2
FirebaseFirestore.instance
    .useFirestoreEmulator(ip, 8080, sslEnabled: false);
await FirebaseAuth.instance.useAuthEmulator(ip, 9099);

I used adb reverse to tunnel the localhost ports to the computer, but it still doesn't work. It seems to ignore the localhost directive and attempts to connect to 10.0.2.2.

1 Answer 1

0

When using an emulator, the Firebase SDK assumes that you're running on an actual Android Studio emulator, and maps localhost to 10.0.2.2 behind the scenes:

//Android considers localhost as 10.0.2.2 - automatically handle this for users.
if (!kIsWeb && defaultTargetPlatform == TargetPlatform.android) {
  if ((mappedHost == 'localhost' || mappedHost == '127.0.0.1') &&
  automaticHostMapping) {
    ignore: avoid_print
    print('Mapping Firestore Emulator host "$mappedHost" to "10.0.2.2".');
    mappedHost = '10.0.2.2';
    }
}

In order to avoid this behavior, set automaticHostMapping to false:

FirebaseFirestore.instance
    .useFirestoreEmulator(ip, 8080, sslEnabled: false, automaticHostMapping: false);
await FirebaseAuth.instance.useAuthEmulator(ip, 9099, automaticHostMapping: false);

Now localhost will actually point to 127.0.0.1, and together with the adb reverse tunnel it will send the traffic to the host machine.

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

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.