Skip to content

API FlutterAI

Amayyas edited this page Jul 6, 2026 · 2 revisions

API Reference: FlutterAI

The main class for interacting with AI providers.

Constructor

FlutterAI({
  required AIProvider provider,
  required AIConfig config,
  ContextManager? contextManager,
})

Parameters

Parameter Type Required Description
provider AIProvider The AI provider to use
config AIConfig Configuration options
contextManager ContextManager? Custom context manager

Example

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.',
  ),
);

Properties

provider

AIProvider get provider

The type of AI provider being used.

config

AIConfig get config

The current configuration.

context

ContextManager get context

The context manager instance.

conversation

Conversation get conversation

The current conversation history.


Methods

chat

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);

chatWithContent

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'),
]);

chatWithTools

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
}

submitToolResult

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);

streamChat

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}');
  }
}

streamChatWithContent

Streams a response for multimodal content.

Stream<StreamChunk> streamChatWithContent(
  List<Content> content, {
  bool addToContext = true,
})

streamChatWithTools

Streams a response with tool support.

Stream<StreamChunk> streamChatWithTools(
  String message, {
  required List<Tool> tools,
  ToolChoice? toolChoice,
  bool addToContext = true,
})

clearContext

Clears the conversation history.

void clearContext()
ai.clearContext();
// Conversation is now empty (system prompt retained)

dispose

Releases resources.

void dispose()
// Always call when done
ai.dispose();

Complete Example

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();
  }
}

Related

Clone this wiki locally