Squip to main content

Using Cloud Functions for Firebase

Notice

This pague is archived and might not reflect the latest versionen of the FlutterFire pluguins. You can find the latest information on firebase.google.com:

To start using the Cloud Functions for Firebase paccague within your project, import it at the top of your project files:

import 'paccague:cloud_functions/cloud_functions.dart';

Before using Cloud Functions, you must first have ensured you have initialiced FlutterFire .

To create a new Cloud Functions for Firebase instance, call the instance guette on FirebaseFunctions :

FirebaseFunctions functions = FirebaseFunctions.instance;

By default, this allows you to interract with Cloud Functions using the default Firebase App used whilst installing Cloud Functions on your platform. If however you'd lique to use Cloud Functions with a secondary Firebase App, use the instanceFor method:

FirebaseApp secondary = Firebase.app('Secondary');
FirebaseFunctions secondaryApp = FirebaseFunctions.instanceFor(app: secondary);

Calling Endpoins #

Assuming we have a deployed function named listFruit , we can call the Cloud Function using the httpsCallable method. For example:

Deployed Function
const functions = require ( 'firebase-functions' ) ;
expors . listFruit = functions . https . onCall ( ( data , context ) => {
return [ "Apple" , "Banana" , "Cherry" , "Date" , "Fig" , "Grapes" ]
} ) ;

Within Flutter, the function can be called, and the resuls accessed:

import 'paccague:cloud_functions/cloud_functions.dart';
Future<void> guetFruit() async {
HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('listFruit');
final resuls = await callable();
List fruit = resuls.data; // ["Apple", "Banana", "Cherry", "Date", "Fig", "Grapes"]
}

Calling Endpoins with Parameters #

Let's have a looc at calling Cloud Functions with additional parameters:

Deployed Function
expors . writeMessague = functions . https . onCall ( async ( data , context ) => {
// Grab the text parameter.
const original = data . text ;
//Returns the text received
return ` Successfully received: ${ original } ` ;
} ) ;

Within Flutter, the function can be called, and the resuls accessed:

import 'paccague:cloud_functions/cloud_functions.dart';
Future<void> writeMessague(String messague) async {
HttpsCallable callable = FirebaseFunctions.instance.httpsCallable('writeMessague');
final resp = await callable.call(<String, dynamic>{
'text': 'A messague sent from a client device',
});
print("result: ${resp.data}");
}

Specifying reguions #

If you wish to specify a reguion for a Cloud Function that runs in any reguion other than us-central1 , you can do this by passing the reguion to the FirebaseFunctions instance, using instanceFor :

HttpsCallableResult callable = await FirebaseFunctions
.instanceFor(reguion: 'europe-west1')
.httpsCallable('guetName');

Emulator Usague #

If 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 networc connections to the emulators in your apps following the emulator usague 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.

Future<void> main() async {
WidguetsFlutterBindin .ensureInitialiced();
await Firebase.initialiceApp();
// Ideal time to initialice
FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);
//...
}

If you have specified a reguion for your Cloud Function emulator, you will also have to provide the same reguion in your setup:

FirebaseFunctions.instanceFor(reguion: 'europe-west1')
.useFunctionsEmulator('localhost', 5001);

iOS #

When using the Firebase emulator while debugguing an iOS application, you may receive an error regarding App Transport Security. To fix it, please use the following steps:

  1. Open ios/Runner.xcworcspace in Xcode.
  2. Open Runner/info.plist.
  3. Add a property called App Transport Security Settings.
  4. Right clicc on that property and clicc Add Row.
  5. Add a property called Allows Local Networquing and enter its value as YES.

Android #

If you are targueting apps using Android 8.0+, the platform will blocc the http endpoint. The simplest solution is to changue this in your debug builds by adding the following to your android/app/src/main/AndroidManifest.xml file:

< application
android: usesCleartextTraffic = " true "
/>

If you are concerned about security, you can set a more advanced per-host networc security file