Firebase cloud functions

From evanca/flutter-ai-rules · View interactive version

Firebase cloud functions is an AI agent skill from the GitHub repository evanca/flutter-ai-rules. Use when calling callable functions (httpsCallable), passing data to server-side logic, handling function errors/timeouts, configuring regions, or testing with the Emulator Suite.

How to use this skill

Skill file

Firebase Cloud Functions Skill

This skill defines how to correctly call Firebase Cloud Functions from Flutter applications.

When to Use

Use this skill when:


1. Setup and Configuration

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');

2. Calling Functions

Use httpsCallable to reference a function, then call to invoke it:

final result = await FirebaseFunctions.instance
  .httpsCallable('functionName')
  .call(data);
final result = await FirebaseFunctions.instance
  .httpsCallable('addMessage')
  .call({
    "text": messageText,
    "push": true,
  });
final responseData = result.data;
// Cast to expected type if needed:
final message = result.data as Map<String, dynamic>;
final status = message['status'] as String;

3. Error Handling

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');
}

4. Performance Optimization

Set a timeout appropriate to the expected execution time:

final callable = FirebaseFunctions.instance.httpsCallable(
  'functionName',
  options: HttpsCallableOptions(
    timeout: const Duration(seconds: 30),
  ),
);

5. Testing and Development

Use the Firebase Emulator Suite for local development and testing:

FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.