flutter_ai_provider_gemini
Native Google Gemini provider for flutter_ai — streaming, function calling, and Google Search grounding → web citations rendered inline.
Family: flutter_ai ·
core · client · elements ·
openai · anthropic
Recipes · Migrating from the Vercel AI SDK
With grounding enabled, answers stream source citations (AiSources / AiInlineCitation).
A native Google Gemini LlmProvider for the
flutter_ai family. It streams Gemini's streamGenerateContent endpoint and
maps each chunk to AiStreamEvents.
Why native (vs. the OpenAI-compatible Gemini endpoint): it speaks Gemini's own
protocol, which unlocks Google Search grounding — grounded answers stream
their web sources back as SourcePart citations (rendered by AiSources /
AiInlineCitation). It also supports function calling and thinking.
Usage
import 'package:flutter_ai_provider_gemini/flutter_ai_provider_gemini.dart';
final provider = GeminiProvider(
apiKey: const String.fromEnvironment('GEMINI_API_KEY'),
enableGrounding: true, // attach the Google Search tool → web citations
);
final controller = UseChatController(
provider: provider,
options: const AiRequestOptions(model: 'gemini-2.5-flash'),
);
Notes
- Grounding: set
enableGrounding: true(or pass agoogleSearchtool viaAiRequestOptions.extra). Grounded responses emitSourceParts for each web source. - Function calling: pass
ToolDefinitions; the model'sfunctionCalls stream as tool-call events, and tool results map tofunctionResponseparts (the function name is recovered from the matching call). - Config:
temperature/maxOutputTokensare sent undergenerationConfig; arbitrary extra request fields go viaAiRequestOptions.extra. - Images: user-message image attachments (
FilePartwith animage/*media type) are sent asinlineData/fileData. Other document types are not yet sent. - Retry: transient failures (429/5xx, network) are retried with backoff
honoring
Retry-After(maxRetries, default 2).
Status
The request/response mapping is unit-tested against recorded SSE chunks. Supply an API key to use it against the live API.
If flutter_ai saves you time, you can buy me a coffee ☕.
Libraries
- flutter_ai_provider_gemini
- Native Google Gemini provider for the
flutter_aifamily.