Google AI SDK for Dart: Unleash the Power of Generative AI

Published on by Flutter News Hub

Google AI SDK for Dart: Unleash the Power of Generative AI

The Google AI Dart SDK grants developers access to Google's cutting-edge generative AI models, such as Gemini. With this SDK, you can effortlessly integrate AI-driven capabilities into your Dart (and Flutter) applications, enabling:

  • Text generation from text input
  • Multimodal text generation (text and images)
  • Conversational chatbots
  • Embedding

Getting Started

1. Obtain an API Key

To commence using the SDK, you'll need an API key. Detailed instructions can be found at https://ai.google.dev/tutorials/setup.

2. Install the Package

Dart:

dart pub add google_generative_ai

Flutter:

flutter pub add google_generative_ai

3. Import the Package

import 'package:google_generative_ai/google_generative_ai.dart';

Using the API

Text Generation Example

const apiKey = ...;

void main() async {
  final model = GenerativeModel(model: 'gemini-pro', apiKey: apiKey);
  final prompt = 'Write a story about a magic backpack.';
  final content = [Content.text(prompt)];
  final response = await model.generateContent(content);
  print(response.text);
}

Additional Resources

Caution

For production use, it's strongly advised to invoke the SDK server-side, safeguarding your API key from exposure.

Flutter News Hub