Google Gemini in Flutter with google_generative_ai
Want a real, multimodal AI assistant in your Flutter app without wiring up a backend? The official google_generative_ai SDK gives you direct access to Google's Gemini models — text, images, and streaming responses — in about twenty lines of Dart.
Why Gemini for Flutter
Gemini is fast, multimodal (it reads images as easily as text), and has a
generous free tier for prototyping. Because google_generative_ai is
maintained by Google, it tracks new model versions quickly and exposes streaming,
function calling, and vision through one clean Dart API.
Installing
dependencies:
google_generative_ai: ^0.4.6
Grab a free API key from Google AI Studio and keep it out of source control (use --dart-define or a secrets file).
Your first Gemini call
import 'package:google_generative_ai/google_generative_ai.dart';
final model = GenerativeModel(
model: 'gemini-1.5-flash',
apiKey: const String.fromEnvironment('GEMINI_KEY'),
);
final response = await model.generateContent(
[Content.text('Give me a witty tagline for a Flutter blog.')],
);
print(response.text);
Streaming responses
For a chat feel, stream tokens as they arrive instead of waiting for the full reply:
final stream = model.generateContentStream(
[Content.text('Explain widgets like I am five.')],
);
await for (final chunk in stream) {
setState(() => _buffer += chunk.text ?? '');
}
Going multimodal
Pass an image alongside text and Gemini will reason about it — perfect for "describe this photo" or receipt parsing:
final bytes = await File(path).readAsBytes();
final res = await model.generateContent([
Content.multi([
TextPart('What is in this image?'),
DataPart('image/jpeg', bytes),
]),
]);
Live demo
Try it: fluttercook.github.io/demos/google_generative_ai · source on github.com/fluttercook.
Gotchas
- Never ship your API key in the client. For production, proxy calls through a backend or Firebase AI with App Check.
- Handle rate limits and safety blocks — wrap calls in try/catch and inspect
promptFeedback. - Pick the right model:
flashfor speed and cost,profor hard reasoning.
Takeaway
google_generative_ai is the fastest way to put a capable, multimodal
LLM into a Flutter app. Start with gemini-1.5-flash, stream the output
for a snappy chat, and move key handling to a backend before you launch.
FlutterCook is publishing 100 hands-on guides to the best open-source Flutter libraries — AI-first. Every demo is runnable at github.com/fluttercook.
Package: pub.dev/packages/google_generative_ai · Tags: Flutter, AI, LLM, Gemini