Integrating Gemini AI with Flutter

Unlocking the Power of Generative AI: Building Richer Flutter Experiences with Gemini

Jaime Sanchez
Jaime Sanchez
February 29, 2024
3min
News
Integrating Gemini AI with Flutter

Now, with the new Beta version of the Gemini Dart SDK plug-in, developers can seamlessly integrate AI capabilities into their Flutter applications.


Let’s dive into the power and best practices of Gemini AI to introduce new functionalities to create much richer digital experiences.

Having access to Google's generative AI SDK allows new possibilities in app development, as well as enhancing existing functionalities. We will showcase how to integrate Gemini AI into a Flutter app.

Getting an API key

Before diving into the code, we have to get an API key from Google AI Studio. If you are having issues trying to access Google AI Studio with a work account, you can either contact your administrator to enable Early Access apps or use a personal Gmail account, since those have the access enabled already. 

Adding the SDK to an app

We will be using the Google Generative AI SDK for Dart, which is already available in pub.dev.

To use the package in your app, add the dependency to your pubspec.yaml file.

dependencies:
google_generative_ai: ^0.2.0

Remember to check the package's latest instructions on how to add it to your project if you are having issues.

Best practices

Now that we have the package available in the app, let's see how to use it effectively. 

Creating a Gemini client

At VGV we believe that a layered architecture is the best way to build maintainable, testable, performant, and scalable code. Following this pattern, we will create a GeminiClient that will be responsible for interacting with the Generative AI SDK and parsing the responses as needed.

class GeminiClient {
GeminiClient({
required this.model,
});

final GenerativeModel model;

Future generateContentFromText({
required String prompt,
}) async {
final response = await model.generateContent([Content.text(prompt)]);
return response.text;
}
}

In this example, we're just showing how to generate content from a text prompt, but we encourage you to play with other types of content such as images with the Content.multi() constructor, or even starting a chat session with Gemini with the  _model.startChat() method!

Provide the API key securely

The API key accesses the model on your behalf, you should keep it safe and private. One way of doing so is by providing it as an environment variable when running our app. First, we will build the logic in our app to retrieve it.

void main(){
final geminiApiKey = Platform.environment['GEMINI_API_KEY'];
if (geminiApiKey == null) {
throw Exception('Gemini API key not found');
}
final model = GenerativeModel(model: 'gemini-pro', apiKey: geminiApiKey);
final gemini = GeminiClient(model: model);

runApp(
Provider(
create: (_) => geminiClient,
child: const App(),
),
);
}

Then, when running your app, provide the key.

flutter run --dart-define="GEMINI_API_KEY="

Organize and add context to prompts

There are plenty of use cases for generative AI to be used in an app, but don’t miss the opportunity to combine user prompts with additional context.

Let’s say that you have a story-teller feature in your app powered by Gemini. The user can prompt a summary of the plot or some events that the story should have, but you, as the developer, can add some context to enhance the story and add an overarching theme before sending the request to Gemini. This and other techniques are explained more deeply in the Gemini API documentation.

Let’s see it in action

Name of the heading

Category
Flutter
Backend
Share

Insights from Our Experts

View All Insights
Generative UI for Flutter: Build Adaptive, Branded, and Intelligent User Experiences

Generative UI for Flutter: Build Adaptive, Branded, and Intelligent User Experiences

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

VGV Team
VGV Team
October 24, 2025
Generative UI for Flutter: Build Adaptive, Branded, and Intelligent User Experiences
Google I/O 2023: Spotlight on AI, Flutter moves forward, and I/O FLIP takes the stage

Google I/O 2023: Spotlight on AI, Flutter moves forward, and I/O FLIP takes the stage

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

May 12, 2023
Google I/O 2023: Spotlight on AI, Flutter moves forward, and I/O FLIP takes the stage
Why we use flutter_bloc for state management

Why we use flutter_bloc for state management

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse varius enim in eros elementum tristique. Duis cursus, mi quis viverra ornare, eros dolor interdum nulla, ut commodo diam libero vitae erat. Aenean faucibus nibh et justo cursus id rutrum lorem imperdiet. Nunc ut sem vitae risus tristique posuere.

Jorge Coca
Jorge Coca
June 23, 2021
Why we use flutter_bloc for state management