Cloud Functions
Cloud Functions allows you to run backend code in response to events triggered by Firebase features and HTTPS requests. For example, you want to automatically send a welcome email to users when they sign up for your app. You can do this using a Cloud Function that triggers on Firebase Authentication's user creation event.
We allow you to write and deploy Firebase Cloud Functions directly within the platform. With an integrated code editor, writing JavaScript cloud functions is quick and user-friendly. Each function has customizable boilerplate settings, including pre-configured essentials like memory, region, and timeout.
Read up on some interesting use cases of Cloud Functions.
Adding Cloud Functions
Let's see how to add a Cloud Function by building an example that generates logos based on user prompts. Here's how it looks:
The Cloud Function takes input from a TextField widget and initiates an API call to an image generation API. Once the image URL is retrieved, it's displayed within an Image widget.
Here are the step-by-step instructions to build such an example:
- Add page state variables
- Build a page
- Create and deploy Cloud Function
- Optional: Add package
- Trigger Cloud Function
- Optional: Use Cloud Function result
- Make sure the project is on Blaze plan on Firebase.
- Completed all steps in the Firebase Setup.
1. Add page state variables
For this example, you'll need to set up two Page State variables:
- generatingImage (Type: Boolean): This is used to control the visibility of a loading indicator during the logo creation process. Its value is set to True before initiating the API call and switched to False once the logo generation is complete.
- logoImage (Type: ImagePath): This is used to hold the generated logo image. After a successful API call, the retrieved image URL is stored here, allowing the logo to be displayed in the Image widget.