Flutter firebase connect to emulator from real device
Asked Answered
M

2

3

Hi I am currently using firebase's local emulator and flutter. However, I am using a real device and not a simulator therefore I do not know how to connect to my laptops localhost. I am currently using this code

  // [Firestore | localhost:8080]
  FirebaseFirestore.instance.settings = const Settings(
    host: "localhost:8080",
    sslEnabled: false,
    persistenceEnabled: false,
  );

  // [Authentication | localhost:9099]
  await FirebaseAuth.instance.useEmulator("http://localhost:9099");

  FirebaseFunctions.instance.useFunctionsEmulator(
      origin: "http://localhost:5001"
  );

  // [Storage | localhost:9199]
  await FirebaseStorage.instance.useEmulator(
    host: "localhost",
    port: 9199,
  );
Mousse answered 14/6, 2021 at 15:56 Comment(0)
M
7

Ok I fixed the problem by these two steps:

firebase.json:

{
  ...
  "emulators": {
    "auth": {
      "host": "0.0.0.0", <--- Adding host
      "port": 9099
    },
    "functions": {
      "host": "0.0.0.0",
      "port": 5001
    },
    "firestore": {
      "host": "0.0.0.0",
      "port": 8080
    },
    "storage": {
      "host": "0.0.0.0",
      "port": 9199
    },
    "ui": {
      "enabled": true
    }
  },
  ...
}

flutter main.dart:

const String localIp = "You local ip goes here";



FirebaseFirestore.instance.settings = const Settings(
  host: localIp+":8080",
  sslEnabled: false,
  persistenceEnabled: false,
);

await FirebaseAuth.instance.useEmulator("http://"+localIp+":9099");

FirebaseFunctions.instance.useFunctionsEmulator(
    origin: "http://"+localIp+":5001"     
);

await FirebaseStorage.instance.useEmulator(
  host: localIp,
  port: 9199,
);
Mousse answered 14/6, 2021 at 17:56 Comment(3)
Thanks! Remember to consider the region if your local functions are set up to a specific region: FirebaseFunctions.instanceFor(region: 'europe-west1').useFunctionsEmulator('192.168.50.43', 5001); cloud_functions@^3.0.3 syntaxCorky
No http://... is actually needed. Your IP address is already enough!Aristocrat
I do not found the firebase.json file in my project directory. Can you help?Streamlined
S
0

Besides the accepted answer, do not forget to also allow unencrypted traffic in your AndroidManifest.xml when developing for Android. See this answer, on how to do it.

Sloe answered 3/7 at 16:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.