android - 从 Flutter 应用通过本地网络连接到 Firebase 函数模拟器

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

我正在尝试从我的测试 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: INTERNAL, {code: unavailable, message: com.google.firebase.functions.FirebaseFunctionsException: INTERNAL}

我做错了什么?

最佳答案

默认情况下,所有模拟器将仅监听本地主机,而不监听您的本地网络。

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

enter image description here

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

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

enter image description here

如果您使用 firebaseserve --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 启动模拟器即可。

关于android - 从 Flutter 应用通过本地网络连接到 Firebase 函数模拟器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68192760/

相关文章:

json - 如何在Flutter列表中使用JSON数据列表创建DropdownButton

swift - 错误 : no such module 'FirebaseInstanceID' on ios

authentication - 如何在Flutter中实现灵活的登录界面?

android - 即使在卸载后也能保持 paypal 应用内购买

Android popBackStack 不加载 ListFragment 中的 ListView

java - 尝试向 Firebase 实时数据库添加密码,但未显示?

android - 在 Firebase 中同时上传到数据库和存储

android - Flutter - 收到通知时更改应用栏图标

android - 当 DATE_ADDED 为 0 时,使用 DATE_ADDED 而不是 DATE_TAKEN 对查询进行排序

android - 为什么MQTT客户端无法重连?