Using Cloud Functions for Firebase
Notice
This page is archived and might not reflect the latest version of the FlutterFire plugins. You can find the latest information on firebase.google.com:
- Writing and deploying Cloud Functions: https://firebase.google.com/docs/functions/get-started
(and on the rest of the pages at
https://firebase.google.com/docs/functions/
) - Calling Cloud Functions from Flutter: https://firebase.google.com/docs/functions/callable#dart
To start using the Cloud Functions for Firebase package within your project, import it at the top of your project files:
Before using Cloud Functions, you must first have ensured you have initialized FlutterFire.
To create a new Cloud Functions for Firebase instance, call the instance
getter on FirebaseFunctions
:
By default, this allows you to interact with Cloud Functions using the default Firebase App used whilst installing Cloud Functions on your
platform. If however you'd like to use Cloud Functions with a secondary Firebase App, use the instanceFor
method:
#
Calling EndpointsAssuming we have a deployed function named listFruit
, we can call the Cloud Function using the httpsCallable
method.
For example:
Within Flutter, the function can be called, and the results accessed:
#
Calling Endpoints with ParametersLet's have a look at calling Cloud Functions with additional parameters:
Within Flutter, the function can be called, and the results accessed:
#
Specifying regionsIf you wish to specify a region for a Cloud Function that runs in any region other than us-central1
, you can do this
by passing the region to the FirebaseFunctions instance, using instanceFor
:
#
Emulator UsageIf you are using the local Cloud Functions emulators, then it is possible to connect to these using the useFunctionsEmulator
method.
Ensure you pass the correct port on which the Firebase emulator is running on.
Ensure you have enabled network connections to the emulators in your apps following the emulator usage instructions in the general FlutterFire installation notes for each operating system. You only have to call useFunctionsEmulator
once, so do it as early as possible.
If you have specified a region for your Cloud Function emulator, you will also have to provide the same region in your setup:
#
iOSWhen using the Firebase emulator while debugging an iOS application, you may receive an error regarding App Transport Security. To fix it, please use the following steps:
- Open ios/Runner.xcworkspace in Xcode.
- Open Runner/info.plist.
- Add a property called App Transport Security Settings.
- Right click on that property and click Add Row.
- Add a property called Allows Local Networking and enter its value as YES.
#
AndroidIf you are targeting apps using Android 8.0+, the platform will block the http endpoint. The simplest solution is to change
this in your debug builds by adding the following to your android/app/src/main/AndroidManifest.xml
file:
If you are concerned about security, you can set a more advanced per-host network security file