Skip to content

Mastering the Flutter Gemini Package

  • by

Flutter Gemini is an advanced package that bridges your Flutter application with Google’s cutting-edge Gemini AI. This package empowers developers to integrate powerful AI capabilities into their apps, enabling innovative, intelligent, and engaging user experiences. This blog post will guide you through setting up and using the Flutter Gemini package, covering various features and advanced usages.

Getting Started

To get started with the Gemini API, you need an API key. If you don’t have one, create a key in Google AI Studio. Once you have your API key, you can begin integrating Gemini into your Flutter application.

Initializing Gemini

To initialize Gemini, you need to call the init constructor in your main function. This sets up the necessary configurations for the Gemini API.

void main() {
  // Add this line
  Gemini.init(apiKey: '--- Your Gemini Api Key ---');

  runApp(const MyApp());

Now you can create an instance of the Gemini class.

Content-Based APIs

The Flutter Gemini package offers several content-based APIs for various use cases. Let’s explore these APIs in detail.

Stream Generate Content

The streamGenerateContent method allows you to receive partial results as the model generates content, speeding up interactions.

final gemini = Gemini.instance;

gemini.streamGenerateContent('Utilizing Google Ads in Flutter')
  .listen((value) {
  }).onError((e) {
    log('streamGenerateContent exception', error: e);

Text-Only Input

Perform natural language processing (NLP) tasks such as text completion and summarization using the text method.

final gemini = Gemini.instance;

gemini.text("Write a story about a magic backpack.")
  .then((value) => print(value?.output)) // or value?.content?.parts?.last.text
  .catchError((e) => print(e));

Text-and-Image Input

Send a text prompt with an image to perform vision-related tasks like captioning or identifying the content of an image.

final gemini = Gemini.instance;

final file = File('assets/img.png');
      text: "What is this picture?", // text
      images: [file.readAsBytesSync()] // list of images
    .then((value) => log(value?.content?.parts?.last.text ?? ''))
    .catchError((e) => log('textAndImageInput', error: e));

Multi-Turn Conversations

Build freeform conversations across multiple turns using the chat method.

final gemini = Gemini.instance;[
  Content(parts: [Parts(text: 'Write the first line of a story about a magic backpack.')], role: 'user'),
  Content(parts: [Parts(text: 'In the bustling city of Meadow brook, lived a young girl named Sophie. She was a bright and curious soul with an imaginative mind.')], role: 'model'),
  Content(parts: [Parts(text: 'Can you set it in a quiet village in 1600s France?')], role: 'user'),
    .then((value) => log(value?.output ?? 'without output'))
    .catchError((e) => log('chat', error: e));

Count Tokens

Count tokens before sending any content to the model using the countTokens method.

final gemini = Gemini.instance;

gemini.countTokens("Write a story about a magic backpack.")
    .then((value) => print(value)) // output like: `6` or `null`
    .catchError((e) => log('countTokens', error: e));

Model Info

Retrieve information about a model such as version, display name, and input token limit using the info method.

final gemini = Gemini.instance; 'gemini-pro')
    .then((info) => print(info))
    .catchError((e) => log('info', error: e));

List Models

List all models available through the API using the listModels method.

final gemini = Gemini.instance;

    .then((models) => print(models)) // list
    .catchError((e) => log('listModels', error: e));

Embed Contents and Batch Embed Contents

Transform text into a list of floating-point numbers (embeddings) to compare similarities between texts.

// `embedContents`
gemini.embedContent('text').then((value) {
  print(value); // output like: [ 1.3231, 1.33421, -0.123123 ]

// `batchEmbedContents`
gemini.batchEmbedContents(['text 1', 'text 2']).then((value) {
  print(value); // output like: [ [ 1.3231, 1.33421, -0.123123 ] ]

Advanced Usage

Safety Settings

Configure what the model blocks and allows in both prompts and responses using the safetySettings argument.

gemini.streamGenerateContent('Utilizing Google Ads in Flutter',
      safetySettings: [
          category: SafetyCategory.harassment,
          threshold: SafetyThreshold.blockLowAndAbove,
          category: SafetyCategory.hateSpeech,
          threshold: SafetyThreshold.blockOnlyHigh,
    .listen((value) {})
    .onError((e) {});

Generation Configuration

Modify generation parameters using the generationConfig argument.

gemini.streamGenerateContent('Utilizing Google Ads in Flutter',
      generationConfig: GenerationConfig(
        temperature: 0.75,
        maxOutputTokens: 512,
  .listen((value) {})
  .onError((e) {});

Flutter Gemini Widgets

The GeminiResponseTypeView widget allows you to present your search results using a typing animation without using the setState() function.

final Widget result = GeminiResponseTypeView(
  builder: (context, child, response, loading) {
    if (loading) {
      // show loading animation or use CircularProgressIndicator();
      return Lottie.asset('assets/lottie/ai.json');

    // The runtimeType of response is String?
    if (response != null) {
      return Markdown(
        data: response,
        selectable: true,
    } else {
      // idle state
      return const Center(child: Text('Search something!'));


The Flutter Gemini package offers a robust set of features to integrate Google’s Gemini AI capabilities into your Flutter applications. From content generation to multi-turn conversations and advanced configurations, this package provides the tools needed to create innovative and intelligent user experiences. For more detailed information and the latest updates, visit the official documentation.

By following this guide, you’ll be able to harness the power of Gemini AI in your Flutter applications, creating richer and more engaging user interactions. Don’t forget to check out other related posts to expand your knowledge on Flutter and AI integration:

Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *