【问题标题】:Connect to Firebase functions emulators over local network from a Flutter app从 Flutter 应用通过本地网络连接到 Firebase 函数模拟器
【发布时间】:2021-09-12 11:51:24
【问题描述】:

我正在尝试从我的测试 Android 设备连接到我的 Firebase 函数模拟器。

当我运行模拟器时,输出是:

┌─────────────────────────────────────────────────────────────┐
│ ✔  All emulators ready! It is now safe to connect your app. │
│ i  View Emulator UI at http://localhost:4000                │
└─────────────────────────────────────────────────────────────┘

┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator  │ Host:Port      │ View in Emulator UI             │
├───────────┼────────────────┼─────────────────────────────────┤
│ Functions │ localhost:5001 │ http://localhost:4000/functions │
└───────────┴────────────────┴─────────────────────────────────┘
  Emulator Hub running at localhost:4400
  Other reserved ports: 4500

Issues? Report them at https://github.com/firebase/firebase-tools/issues and attach the *-debug.log files.

在我的 Flutter 应用中,我执行以下操作来连接:

FirebaseFunctions.instance.useFunctionsEmulator(origin: 'http://192.168.1.158:5001');

我已将android:usesCleartextTraffic="true" 添加到我的AndroidManifest 和network_security_config.xml,如herehere 所述。

我总是收到以下错误:

PlatformException(firebase_functions,com.google.firebase.functions.FirebaseFunctionsException:内部,{代码:不可用,消息:com.google.firebase.functions.FirebaseFunctionsException:内部}

我做错了什么?

【问题讨论】:

  • origin: 'http://localhost:5001' 工作吗?
  • 不@Dharmaraj,这是一个物理设备。
  • 那你可以试试firebase serve --host 0.0.0.0 让模拟器在网络上可用吗?

标签: android firebase flutter google-cloud-functions firebase-tools


【解决方案1】:

默认情况下,所有模拟器将只监听 localhost 而不是您的本地网络。

我尝试通过在整个网络上运行我的主机模拟器和仅在本地主机上运行模拟器来复制您的问题,如下面的屏幕截图所示。

const firebaseConfig = {...}
firebase.initializeApp(firebaseConfig);

firebase.functions().useEmulator("192.168.0.102", 5001);

var addMessage = firebase.functions().httpsCallable('addMessage');
addMessage({ text: "messageText" }).then((result) => {
  // Read result of the Cloud Function.
  var sanitizedMessage = result.data.text;
}); 

我从文档中复制了示例函数并尝试调用该函数,结果符合预期:

如果您使用 firebase serve --only functions --host 0.0.0.0 提供功能,它应该使功能可用于您的网络。

或者,您可以像这样在firebase.json 中指定:

{
  "functions": {
    "predeploy": "npm --prefix \"$RESOURCE_DIR\" run build"
  },
  "emulators": {
    "functions": {
      "port": 5001,
      "host": "0.0.0.0"
    }
  }
}

然后您可以使用firebase emulators:start 简单地启动模拟器。

【讨论】:

  • YES.. 我喜欢你提到的:然后你可以简单地使用 firebase emulators:start 启动模拟器。我只是在更改主机但没有重新启动模拟器!
猜你喜欢
  • 2020-11-09
  • 2015-12-19
  • 2020-02-04
  • 2014-10-15
  • 2021-12-16
  • 1970-01-01
  • 2020-07-15
  • 2021-02-26
相关资源
最近更新 更多