This skill defines how to correctly call Firebase Cloud Functions from Flutter applications.
Use this skill when:
flutter pub add cloud_functions
import 'package:cloud_functions/cloud_functions.dart';
// After Firebase.initializeApp():
final functions = FirebaseFunctions.instance;
final functions = FirebaseFunctions.instanceFor(region: 'europe-west1');
Use httpsCallable to reference a function, then call to invoke it:
final result = await FirebaseFunctions.instance
.httpsCallable('functionName')
.call(data);
Map — it is automatically serialized to JSON:final result = await FirebaseFunctions.instance
.httpsCallable('addMessage')
.call({
"text": messageText,
"push": true,
});
data property — it is automatically deserialized from JSON:final responseData = result.data;
// Cast to expected type if needed:
final message = result.data as Map<String, dynamic>;
final status = message['status'] as String;
Always wrap function calls in try-catch and check for FirebaseFunctionsException:
try {
final result = await FirebaseFunctions.instance
.httpsCallable('functionName')
.call(data);
// Handle successful result
} on FirebaseFunctionsException catch (e) {
switch (e.code) {
case 'not-found':
// Function does not exist
break;
case 'permission-denied':
// User lacks permission
break;
case 'unavailable':
// Service temporarily unavailable — retry
break;
default:
debugPrint('Function error [${e.code}]: ${e.message}');
}
} catch (e) {
debugPrint('Unexpected error: $e');
}
unavailable, deadline-exceeded).Set a timeout appropriate to the expected execution time:
final callable = FirebaseFunctions.instance.httpsCallable(
'functionName',
options: HttpsCallableOptions(
timeout: const Duration(seconds: 30),
),
);
Use the Firebase Emulator Suite for local development and testing:
FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);