Expo 不适用于 Firebase 模拟器

问题描述

我正在尝试通过我的 Expo 应用程序在物理和模拟设备上访问 firestore 模拟器(本地托管),但两者都不起作用。当我调用模拟的 firestore 数据库时,例如设置文档,它不会显示在 UI 上。但是,如果我为真正的 firestore 运行完全相同的代码,它会正常工作。

我的 firebase.json 配置是:

{
  "firestore": {
    "rules": "firestore.rules","indexes": "firestore.indexes.json"
  },"emulators": {
    "firestore": {
      "port": 8080
    },}
}

对于 android 模拟器上的 Expo 应用程序,我将配置设置为:

firebase.firestore().settings({
  host: "10.0.2.2:8080",ssl: false,})

对于我的物理 Android 设备上的 Expo 应用程序:

firebase.firestore().settings({
  host: "localhost:8080",//I've also tried "192.168.68.109:8080" my computer's IP
  ssl: false,})

解决方法

事实证明,您需要将 firebase.json 上的主机指定为

func setupNavBarBtn(){
        
        let backBtn = UIButton(type: .system)
        backBtn.setImage(#imageLiteral(resourceName: "cellarrow").withRenderingMode(.alwaysOriginal),for: .normal)
        backBtn.contentMode = .scaleAspectFit
        backBtn.frame = CGRect(x: 0,y: 0,width: 30,height: 30)

        let callBtn = UIButton(type: .system)
        callBtn.setImage(#imageLiteral(resourceName: "Call").withRenderingMode(.alwaysOriginal),for: .normal)
        callBtn.contentMode = .scaleAspectFit
        callBtn.frame = CGRect(x: 0,height: 30)

        self.navigationItem.leftBarButtonItems = [UIBarButtonItem(customView: backBtn),UIBarButtonItem(customView: callBtn)]

}

对于物理设备上的 Expo 应用程序的配置,不要使用“localhost:8080”,而是将您的计算机(或您网络中的任何主机)IP 地址后跟“:8080”。

如果这一切仍然不起作用,请尝试在您的项目目录中执行命令“firebase use ”,它应该会起作用。

,

如果您在物理设备上进行测试,则使用计算机的 IP 地址进行连接会更好。您可以使用内置变量,而不是对其进行硬编码:Constants.manifest.debuggerHost?.split(":").shift()

在这里写了一个简短的解释:

https://dev.to/haydenbleasel/using-the-firebase-local-emulator-with-expo-s-managed-workflow-5g5k