如firestore文档中所述,我可以通过添加以下部分在firestore.json中设置firestore模拟器的端口:
"emulators": {
"firestore": {
"port": 1234
}
}
但是我该如何连接它呢?initializeTestApp和initializeAdminApp是否将端口作为参数?
我在文档中找不到这个,但可以通过查找@firebase/rules-unit-testing
npm包的源代码来找到它。
您所需要做的就是设置FIRESTORE_EMULATOR_HOST
环境变量:
FIRESTORE_EMULATOR_HOST=localhost:8181
然后只需使用文档中描述的initializeTestApp()
/initializeAdminApp()
。
(如果您使用RealtimeDatabase,看起来您也可以使用DATABASE_ADDRESS_ENV
环境变量。(
参见:
- https://firebase.google.com/docs/emulator-suite/connect_and_prototype#instrument_your_app_to_talk_to_the_emulators
// Initialize your Web app as described in the Get started for Web // firebaseApp previously initialized using firebase.initializeApp(). var db = firebaseApp.firestore(); if (location.hostname === "localhost") { // Note that the Firebase Web SDK must connect to the WebChannel port db.settings({ host: "localhost:8081", ssl: false }); }
你能试试下面的样品吗?
- https://google.dev/pathways/firebase-emulators
- https://google.dev/codelabs/firebase-emulator-get-started#0
我查看了firebase模拟器文档,并在Flutter应用程序的firebase.json
文件中设置了一个不同的端口(5010和5000(。立竿见影的成功。