Flutter Firebase如何从真实设备连接到模拟器

3

你好,我目前正在使用Firebase的本地模拟器和Flutter。但是,我使用的是真实设备而不是模拟器,因此我不知道如何连接到我的笔记本电脑的本地主机。我目前正在使用以下代码:

  // [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,
  );
1个回答

6

好的,我通过以下两个步骤解决了这个问题:

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,
);

谢谢!请记得考虑区域,如果您的本地函数设置为特定区域: FirebaseFunctions.instanceFor(region: 'europe-west1').useFunctionsEmulator('192.168.50.43', 5001); cloud_functions@^3.0.3 语法 - maganap
实际上不需要任何 http://...。你的 IP 地址已经足够了! - Paul
我在我的项目目录中找不到firebase.json文件。你能帮忙吗? - undefined

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接