-
Notifications
You must be signed in to change notification settings - Fork 0
FAQ
Common questions about Flutter AI SDK.
Flutter AI SDK is a unified Dart/Flutter wrapper for integrating multiple AI APIs (OpenAI, Anthropic, Google AI) with a single, consistent interface.
- OpenAI - GPT-5.5, GPT-5.4, GPT-5.4 mini/nano
- Anthropic - Claude Opus 4.8, Sonnet 5, Haiku 4.5
- Google AI - Gemini 3.5 Flash, Gemini 3.1 Pro/Flash-Lite
The SDK itself is open source and free. However, you need API keys from the respective providers, which have their own pricing.
Add to your pubspec.yaml:
dependencies:
flutter_ai_sdk: ^1.0.0Then run flutter pub get.
- Dart SDK: >=3.0.0 <4.0.0
- Flutter: >=3.10.0
Yes! Android, iOS, Web, macOS, Windows, and Linux are all supported.
- OpenAI: platform.openai.com/api-keys
- Anthropic: console.anthropic.com
- Google AI: aistudio.google.com/app/apikey
Never commit API keys to version control. Options:
- Environment variables
- Flutter's
--dart-define - Secure storage packages
- Backend proxy
// Using dart-define
// Run: flutter run --dart-define=OPENAI_KEY=sk-...
const apiKey = String.fromEnvironment('OPENAI_KEY');Simply change the provider parameter:
// OpenAI
final ai = FlutterAI(
provider: AIProvider.openai,
config: AIConfig(apiKey: openaiKey),
);
// Anthropic
final ai = FlutterAI(
provider: AIProvider.anthropic,
config: AIConfig(apiKey: anthropicKey),
);
// Google AI
final ai = FlutterAI(
provider: AIProvider.googleAI,
config: AIConfig(apiKey: googleKey),
);await for (final chunk in ai.streamChat('Tell me a story')) {
if (chunk.isDelta) {
print(chunk.delta);
}
}final response = await ai.chatWithContent([
const TextContent('What is in this image?'),
const ImageContent.fromUrl('https://example.com/image.jpg'),
]);final tool = Tool(
name: 'get_weather',
description: 'Get weather for a location',
parameters: ToolParameters(
properties: {
'location': ToolProperty.string(description: 'City name'),
},
required: ['location'],
),
);
final response = await ai.chatWithTools(
'What is the weather in Paris?',
tools: [tool],
);The SDK automatically maintains conversation history. Each message is added to the context for multi-turn conversations.
ai.clearContext();final messages = ai.conversation.messages;
for (final msg in messages) {
print('${msg.role}: ${msg.textContent}');
}Yes, use addToContext: false:
final response = await ai.chat(
'What is 2+2?',
addToContext: false,
);- Check your API key is correct
- Ensure the key hasn't expired
- Verify you have credits/quota available
You've exceeded the provider's rate limit. Wait and retry:
try {
await ai.chat('Hello');
} on AIRateLimitError catch (e) {
if (e.retryAfter != null) {
await Future.delayed(e.retryAfter!);
// Retry
}
}Your conversation history exceeds the model's context window. Clear context:
try {
await ai.chat(message);
} on AIContextLengthError {
ai.clearContext();
await ai.chat(message);
}It depends on your use case:
| Use Case | Recommended |
|---|---|
| Complex reasoning | GPT-5.5, Claude Opus 4.8 |
| Fast responses | GPT-5.4-mini, Claude Haiku 4.5, Gemini 3.5 Flash |
| Long documents | Claude Opus 4.8, Gemini 3 (1M tokens) |
| Code generation | GPT-5.5, Claude Sonnet 5 |
| Cost-effective | GPT-5.4-nano, Claude Haiku 4.5, Gemini 3.1 Flash-Lite |
final config = AIConfig(
apiKey: 'your-key',
model: 'gpt-5.5',
);- Use streaming for perceived faster responses
- Use faster models (GPT-5.4-mini, Haiku, Flash)
- Reduce
maxTokenswhen possible - Use a region-close provider
- Use cost-effective models
- Limit
maxTokens - Clear context when appropriate
- Use token counting before requests
// Set log level
AILogger.level = LogLevel.verbose;Enable verbose logging to see request/response details.
Yes, use baseUrl:
final config = AIConfig(
apiKey: 'your-key',
baseUrl: 'https://your-proxy.com/v1',
);Yes:
final config = AIConfig(
apiKey: 'your-key',
headers: {
'X-Custom-Header': 'value',
},
);Open an issue on GitHub with:
- SDK version
- Flutter version
- Provider used
- Steps to reproduce
- Error messages
Still have questions? Check the wiki or open a discussion.
Getting Started
Core Concepts
Advanced Features
API Reference
Examples
Other