Reputation: 521
I am developing a flutter app. I'm trying to use the Firebase emulator as a backend during development.
I configured the emulator, and when I used the “firebase emulators:start” command, I confirmed that the following response came out.
Emulator Host:Port View in Emulator UI
Authentication 0.0.0.0:9099 | http://127.0.0.1:4000/auth
Functions 0.0.0.0:5001 | http://127.0.0.1:4000/functions
Firestore 0.0.0.0:8080 | http://127.0.0.1:4000/firestore
Storage 0.0.0.0:9199 | http://127.0.0.1:4000/storage
So, I coded the part that initializes the Firebase settings as follows.
Future initializeApp() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseAuth.instance.useAuthEmulator("0.0.0.0", 9099);
FirebaseFunctions.instance.useFunctionsEmulator("0.0.0.0", 5001);
FirebaseStorage.instance.bucket = 'default-bucket';
FirebaseStorage.instance.useStorageEmulator("0.0.0.0", 9199);
FirebaseFirestore.instance.useFirestoreEmulator("0.0.0.0", 8080);
FirebaseFirestore.instance.settings = const Settings(
host: "0.0.0.0",
persistenceEnabled: false,
sslEnabled: false,
);
}
My firebase.json is below
{
"functions": {
"predeploy": "npm --prefix \"$RESOURCE_DIR\" run build",
"source": "functions"
},
"emulators": {
"auth": {
"host": "0.0.0.0",
"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
}
},
"firestore": {
"rules": "firestore.rules",
"indexes": "firestore.indexes.json"
},
"storage": {
"rules": "storage.rules"
}
}
But when I try to use firestore, it says "Unhandled Exception: [cloud_firestore/unavailable] The service is currently unavailable. This is a most likely a transient condition and may be corrected by retrying with a backoff.".
I think I've done all the settings. I can't figure out which part is wrong. hope for help
Below is the firestore-debug.log file.
Nov 25, 2022 9:34:42 AM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketServer start
INFO: Started WebSocket server on ws://0.0.0.0:9150
API endpoint: http://0.0.0.0:8080
If you are using a library that supports the FIRESTORE_EMULATOR_HOST environment variable, run:
export FIRESTORE_EMULATOR_HOST=0.0.0.0:8080
Dev App Server is now running.
Nov 25, 2022 9:34:44 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Nov 25, 2022 9:34:45 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Nov 25, 2022 9:34:48 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Nov 25, 2022 9:34:48 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected HTTP/2 connection.
Nov 25, 2022 9:35:46 AM com.google.cloud.datastore.emulator.firestore.websocket.WebSocketChannelHandler initChannel
INFO: Connected to new websocket client
Nov 25, 2022 9:35:46 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
Nov 25, 2022 9:36:46 AM io.gapi.emulators.netty.HttpVersionRoutingHandler channelRead
INFO: Detected non-HTTP/2 connection.
*** shutting down gRPC server since JVM is shutting down
*** server shut down
Strange thing is, the Auth Emulator works just fine.
Upvotes: 2
Views: 570
Reputation: 521
I found the answer. I had to change the order of the code.
Future initializeApp() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
FirebaseAuth.instance.useAuthEmulator("0.0.0.0", 9099);
FirebaseFunctions.instance.useFunctionsEmulator("0.0.0.0", 5001);
FirebaseStorage.instance.bucket = 'default-bucket';
FirebaseStorage.instance.useStorageEmulator("0.0.0.0", 9199);
FirebaseFirestore.instance.useFirestoreEmulator("0.0.0.0", 8080);
FirebaseFirestore.instance.settings = const Settings(
host: "0.0.0.0",
persistenceEnabled: false,
sslEnabled: false,
);
}
Changing this code to the following works:
Future initializeApp() async {
await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform,
);
await FirebaseAuth.instance.useAuthEmulator("0.0.0.0", 9099);
FirebaseFunctions.instance.useFunctionsEmulator("0.0.0.0", 5001);
FirebaseStorage.instance.bucket = 'default-bucket';
FirebaseFirestore.instance.settings = const Settings(
host: "0.0.0.0",
persistenceEnabled: false,
sslEnabled: false,
);
FirebaseFirestore.instance.useFirestoreEmulator("0.0.0.0", 8080);
await FirebaseStorage.instance.useStorageEmulator("0.0.0.0", 9199);
}
I think, I need to set “FirebaseFirestore.instance.settings ” first and then "FirebaseFirestore.instance.useFirestoreEmulator"
Upvotes: 0