这不应该连接到 firebase 本地模拟器而不是生产服务器吗?



我有一个使用firebase的flutter应用程序。我想尝试一个特定的类,因此我想为这个类使用本地firestorm模拟器。初始化类时,我正在尝试下面的代码,但由于某种原因,连接仍然直接连接到生产服务器。/hello文档是在生产服务器中创建的,而不是在模拟器中创建的。

if (bool.fromEnvironment('dart.vm.product') == false) {
final FirebaseFirestore db = FirebaseFirestore.instance;
db.useFirestoreEmulator(host, 8050);
db.settings =
const Settings(persistenceEnabled: false, sslEnabled: false);
db.doc('hello/1').set({'text': 1});
print(db.settings);
}

这不是应该连接到本地模拟器吗?

我使用以下连接到本地Firestore模拟器,它可以工作:

if (!kIsWeb) {
FirebaseFirestore.instance.settings = Settings(
host: (Platform.isAndroid ? '10.0.2.2' : 'localhost') + ':8080',
sslEnabled: false,
persistenceEnabled: false);
}

注:

  1. 我知道文档建议了你尝试使用的方法,但我对此有问题,上面的代码对我有效。可以有更好的解决方案
  2. 调用await Firebase.initializeApp();后立即放入此代码
  3. 我不确定最近引入的仅Dart的init,但在几个月前的一个项目中,上述内容不适用于web平台。我不得不将其添加到index.html:firebase.firestore().useEmulator('localhost', 8080);中的脚本中

最新更新