Using AI to your app using Gemini

Flutter tips Published on

Adding powerful AI features to your app can make it smarter and more engaging. This guide shows you a way to use Google's Gemini AI, but instead of adding it directly with a Flutter plugin, we use Firebase Functions. This method keeps your sensitive API key safe on your server, not directly in your app.

Why Use Firebase Functions?

Security is a big reason. By keeping your Gemini API key in Firebase Functions, you avoid putting it into your app's code, which is less secure.

Steps to Add Gemini with Firebase

Here's how you can set this up:

1. Set up Firebase and Dependencies

Start a new Firebase project or use an existing one. You'll need to initialize genkit in your project using the firebase init genkit command. Make sure you have Node.js version 18 or higher installed. You also need these packages:

2. Install Genkit Globally

Open your terminal and run this command:

npm install -g genkit

3. Configure Genkit

In your Firebase project, set up Genkit. You'll need to configure it to use Firebase and Google AI. Importantly, you must add your GOOGLE_GENAI_API_KEY to a .env file in your Firebase functions directory. Make sure your .gitignore file is set up so this .env file is not accidentally uploaded.

How the Firebase Function Works

You create a function, like suggestionFlow shown in the example, that runs on Firebase. This function:

Calling the Function from Flutter

In your Flutter app, you use the Firebase Functions package to call your deployed function (suggestionFlow). You pass the necessary info (like user ID, what they are asking for, and their language). When you get the response back, you can easily read the data, especially if you asked the AI to send it back in JSON format.

Deploy and Test

After creating your function, deploy it to Firebase using firebase deploy --only functions. You can also test your function on your computer locally using the genkit start command (remember to set up your API key locally for testing).

This method gives you a secure way to bring the power of Gemini AI into your Flutter app.

Save 3 months of work

Create your app using our 6 years of making Flutter apps and more than 50+ apps

kickstarter for flutter apps

Frequently Asked Questions

Why shouldn't I use the flutter_gemini plugin directly?

Using a plugin directly often means putting your API key into your app's code. Keeping the key on a secure server like Firebase Functions is much safer as it's not easily accessible if someone inspects your app.

What are the main dependencies needed for this method?

You need several Genkit-related packages (like `@genkit-ai/ai`, `@genkit-ai/firebase`, etc.) and `zod` for defining data types. You also need Node.js version 18+.

Where do I put my Google Gemini API key?

Your API key should be stored in a `.env` file within your Firebase Functions directory. Make sure this file is not included in your version control (like Git) by adding it to your `.gitignore`.

How do I deploy the Firebase function?

You deploy the function using the Firebase CLI command: `firebase deploy --only functions`.

Can I test the function before deploying?

Yes, you can test your function locally using the `genkit start` command from your Firebase Functions directory. Remember to set up your API key for the local environment too.

Read more
You may also be interested in
Guide Users Easily: Tutorial Overlays in Flutter with pal_widgets  blog card image
Guide Users Easily: Tutorial Overlays in Flutter with pal_widgets
Published on 2025-05-12T08:10:13.136Z
AnimatedSwitcher with Riverpod  blog card image
AnimatedSwitcher with Riverpod
Published on 2025-05-12T08:59:30.176Z
ApparenceKit is a flutter template generator tool by Apparence.io © 2025.
All rights reserved