Getting started with the Gemini API and Dart and Flutter

Learn how to use the Gemini API and the Google AI Dart SDK to prototype generative AI in Dart and Flutter applications.

 

Overview: Getting started with the Gemini API and Flutter

Use the Google AI Dart SDK to make your first generative AI call using the Gemini API, build an app using Dart and Flutter, and explore cross-platform sample applications.

Calling the Gemini API directly from your app using the Google AI Dart SDK is only recommended for prototyping and exploring the Gemini generative AI model. You risk exposing the API key to malicious actors if it is embedded or retrieved by your mobile application. For production and enterprise-scale apps, access the Gemini API server-side to keep your API key safe or migrate to Vertex AI in Firebase instead, which offers additional security features and integration into the Google Cloud ecosystem.

Introduction to the Gemini API and prompt engineering

Pathway

Explore Google AI Studio and the capabilities of the Gemini generative AI model. Learn how to design and test the different types of prompts (freeform, structured, and chat) and get an API key for the Gemini API.

This pathway can be useful for further experimentation with the Gemini API and lays the groundwork for integrating its features into your application. Optionally, you can also try out the API using a simple NodeJS web application. If you don't already have NodeJS and NPM on your machine, feel free to skip this step and return back to Dart and Flutter in this pathway.

There are additional security considerations for using API keys in mobile and web client applications. Migrate to Vertex AI in Firebase for production use beyond prototyping for client apps.

Run the Google AI SDK sample on DartPad

Code sample

Try out a Flutter demo of the Google AI Dart SDK on DartPad.

This interactive demo shows how to build a chat app in Flutter that uses the multi-turn conversations functionality from the SDK. Learn how to implement the user interface and manage the state of the conversation.

Enter your Google AI API key when prompted to get started.

Gemini API and Flutter: Practical, AI-Driven apps with Google AI tools

Video

Watch this talk from Google I/O 2024 to get an overview over generative AI, Google AI Studio and prompt design.

Follow along to integrate the Google AI SDK into a Flutter application and build a recipe application that uses the Gemini 1.5 Pro model with multimodal prompts.

Introduction to the Google AI Dart SDK

Article

The Google AI Dart SDK is a Dart-first, cross-platform SDK for building your generative AI integration with the Google AI Gemini API. This SDK also supports Flutter on all platforms.

Note that calling the Gemini API directly from your app using the Google AI Dart SDK is only recommended for prototyping. For production and enterprise-scale apps, access the Gemini API server-side to keep your API key safe or migrate to Vertex AI in Firebase instead.

To get started with the Google AI Dart SDK, set up a project in Google AI Studio, which includes obtaining an API key for the Gemini API. Next, add the required dependencies to your app's pubspec.yaml (google_generative_ai). Then, you can initialize the library with your API key and make your first API call.

You can also check out this YouTube Short for a quick overview over the Google AI Dart SDK and how to get started.

Explore the Dart SDK and Flutter sample apps

Code sample

Explore the generative AI example apps for the Google AI Dart SDK for Flutter and Dart.

The Dart code samples demonstrate three key use cases: generating text, photo reasoning (using multimodal inputs), and multi-turn conversations (chat). It also shows advanced topics, such as how to use content streaming to improve response time by displaying partial results.

The Flutter sample app demonstrates how to implement multi-turn conversations (chat) and photo reasoning (using multimodal inputs) in a multi-platform application.

Follow the steps in the README for each sample to get started, which includes configuring your Google AI API Key and providing it as an environment variable.

Multimodal prompting using the Google AI Dart SDK

Article

Multimodal prompts combine different types of media together, such as text, images and audio. For example, you could create prompts that identify objects in an image, extract text from a photo, or reference a picture.

To get started, read this guide about file prompting strategies and multimodal concepts, which includes best practices for designing multimodal prompts.

Next, explore the multimodal capabilities of the Gemini models in Google AI Studio by uploading or selecting a file as part of your prompt.

Learn how to use multimodal inputs using the Google AI Dart SDK, find image requirements for prompts, and explore the multimodal image chat demo in the Flutter sample app or in the Dart sample scripts.

For further reading, see the solution Leveraging the Gemini Pro Vision model for image understanding, multimodal prompts and accessibility.

Launch your app and move to production with Vertex AI in Firebase

Article

When you are ready to launch your application in production, transition to Vertex AI in Firebase and its SDK for Flutter.

When you want to call the Gemini API directly from your mobile/web Flutter app, we recommend using Vertex AI in Firebase. It offers enhanced security features for mobile and web apps, including integration with Google Cloud IAM for API authorization and Firebase App Check for integrity verification. It integrates with Google's wider Firebase developer platform (like Cloud Storage and Remote Config), while also giving you access to the tools, workflows and scale offered through Google Cloud. Among other differences, Vertex AI also supports increased request quotas and enterprise features.

Note that using the Google AI Dart SDK to call the Gemini API directly from a mobile/web client is only recommended for prototyping and experimentation. Follow this guide to migrate to the Vertex AI in Firebase SDK by updating your package dependencies, imports, and changing how the AI model is initialized.

Vertex AI in Firebase is currently in public preview. Make sure to check the docs for the current release status of the SDKs since the team is committed to making these SDKs generally available as soon as possible!