-
Notifications
You must be signed in to change notification settings - Fork 0
API FlutterAI
Amayyas edited this page Jul 6, 2026
·
2 revisions
The main class for interacting with AI providers.
FlutterAI({
required AIProvider provider,
required AIConfig config,
ContextManager? contextManager,
})| Parameter | Type | Required | Description |
|---|---|---|---|
provider |
AIProvider |
✅ | The AI provider to use |
config |
AIConfig |
✅ | Configuration options |
contextManager |
ContextManager? |
❌ | Custom context manager |
final ai = FlutterAI(
provider: AIProvider.openai,
config: AIConfig(
apiKey: 'your-api-key',
model: 'gpt-5.5',
temperature: 0.7,
systemPrompt: 'You are a helpful assistant.',
),
);AIProvider get providerThe type of AI provider being used.
AIConfig get configThe current configuration.
ContextManager get contextThe context manager instance.
Conversation get conversationThe current conversation history.
Sends a simple text message.
Future<AIResponse> chat(
String message, {
bool addToContext = true,
})| Parameter | Type | Default | Description |
|---|---|---|---|
message |
String |
- | The message to send |
addToContext |
bool |
true |
Whether to add to conversation history |
Returns: Future<AIResponse>
final response = await ai.chat('What is Flutter?');
print(response.text);
print(response.usage?.totalTokens);Sends multimodal content (text, images, etc.).
Future<AIResponse> chatWithContent(
List<Content> content, {
bool addToContext = true,
})| Parameter | Type | Default | Description |
|---|---|---|---|
content |
List<Content> |
- | The content to send |
addToContext |
bool |
true |
Whether to add to history |
final response = await ai.chatWithContent([
const TextContent('What is in this image?'),
const ImageContent.fromUrl('https://example.com/image.jpg'),
]);Sends a message with function/tool support.
Future<AIResponse> chatWithTools(
String message, {
required List<Tool> tools,
ToolChoice? toolChoice,
bool addToContext = true,
})| Parameter | Type | Default | Description |
|---|---|---|---|
message |
String |
- | The message to send |
tools |
List<Tool> |
- | Available tools |
toolChoice |
ToolChoice? |
null |
How to use tools |
addToContext |
bool |
true |
Add to history |
final response = await ai.chatWithTools(
'What is the weather in Paris?',
tools: [weatherTool],
);
if (response.hasToolCalls) {
// Handle tool calls
}Submits the result of a tool call back to the AI.
Future<AIResponse> submitToolResult({
required String toolCallId,
required String name,
required dynamic result,
bool isError = false,
})| Parameter | Type | Default | Description |
|---|---|---|---|
toolCallId |
String |
- | ID from the tool call |
name |
String |
- | Tool name |
result |
dynamic |
- | Tool execution result |
isError |
bool |
false |
Whether result is an error |
final finalResponse = await ai.submitToolResult(
toolCallId: 'call_123',
name: 'get_weather',
result: {'temperature': 22, 'condition': 'sunny'},
);
print(finalResponse.text);Streams a response for a text message.
Stream<StreamChunk> streamChat(
String message, {
bool addToContext = true,
})await for (final chunk in ai.streamChat('Tell me a story')) {
if (chunk.isDelta) {
stdout.write(chunk.delta);
}
if (chunk.isDone) {
print('\nTokens: ${chunk.usage?.totalTokens}');
}
}Streams a response for multimodal content.
Stream<StreamChunk> streamChatWithContent(
List<Content> content, {
bool addToContext = true,
})Streams a response with tool support.
Stream<StreamChunk> streamChatWithTools(
String message, {
required List<Tool> tools,
ToolChoice? toolChoice,
bool addToContext = true,
})Clears the conversation history.
void clearContext()ai.clearContext();
// Conversation is now empty (system prompt retained)Releases resources.
void dispose()// Always call when done
ai.dispose();import 'package:flutter_ai_sdk/flutter_ai_sdk.dart';
Future<void> main() async {
// Initialize
final ai = FlutterAI(
provider: AIProvider.openai,
config: AIConfig(
apiKey: 'your-api-key',
model: 'gpt-5.5',
temperature: 0.7,
maxTokens: 1000,
systemPrompt: 'You are a helpful assistant.',
),
);
try {
// Simple chat
final response1 = await ai.chat('Hello!');
print('AI: ${response1.text}');
// Multimodal chat
final response2 = await ai.chatWithContent([
const TextContent('Describe this:'),
const ImageContent.fromUrl('https://example.com/image.jpg'),
]);
print('AI: ${response2.text}');
// Streaming
print('\nStreaming response:');
await for (final chunk in ai.streamChat('Tell me a joke')) {
if (chunk.isDelta) stdout.write(chunk.delta);
}
print();
// With tools
final response3 = await ai.chatWithTools(
'What is 25 * 4?',
tools: [calculatorTool],
);
if (response3.hasToolCalls) {
for (final call in response3.toolCalls!) {
final result = await executeCalculation(call.arguments);
final finalResponse = await ai.submitToolResult(
toolCallId: call.id,
name: call.name,
result: result,
);
print('AI: ${finalResponse.text}');
}
}
// Check context
print('\nContext: ${ai.conversation.length} messages');
print('Tokens: ${ai.context.estimatedTokens}');
} finally {
ai.dispose();
}
}- AIConfig - Configuration options
- AIResponse - Response structure
- Configuration - Configuration guide
Getting Started
Core Concepts
Advanced Features
API Reference
Examples
Other