Flutter firebase 从真实设备连接到模拟器

问题描述 投票:0回答:2

嗨,我目前正在使用 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,
  );
firebase flutter google-cloud-functions firebase-tools
2个回答
7
投票

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

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

0
投票

除了已接受的答案之外,在为 Android 开发时,不要忘记在您的

AndroidManifest.xml
中允许未加密的流量。请参阅this答案,了解如何操作。

© www.soinside.com 2019 - 2024. All rights reserved.