Skip to content

Quick Start Guide

Amayyas edited this page Jul 6, 2026 · 2 revisions

Quick Start Guide

Get up and running with Flutter AI SDK in minutes!

Step 1: Initialize the SDK

import 'package:flutter_ai_sdk/flutter_ai_sdk.dart';

// Create an instance with your preferred provider
final ai = FlutterAI(
  provider: AIProvider.openai,
  config: AIConfig(
    apiKey: 'your-api-key',
    model: 'gpt-5.5',
  ),
);

Step 2: Send a Simple Message

// Send a chat message
final response = await ai.chat('Hello! How are you?');

// Get the response text
print(response.text);

// Check token usage
print('Tokens used: ${response.usage?.totalTokens}');

Step 3: Stream Responses (Optional)

// Stream responses for real-time output
await for (final chunk in ai.streamChat('Tell me a story')) {
  if (chunk.isDelta) {
    print(chunk.delta); // Print each chunk as it arrives
  }
}

Step 4: Clean Up

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

Complete Example

Here's a complete example you can copy and run:

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,
      systemPrompt: 'You are a helpful assistant.',
    ),
  );

  try {
    // Simple chat
    final response = await ai.chat('What is Flutter?');
    print('AI: ${response.text}');

    // Follow-up (context is maintained)
    final followUp = await ai.chat('Can you give me a code example?');
    print('AI: ${followUp.text}');

  } catch (e) {
    print('Error: $e');
  } finally {
    ai.dispose();
  }
}

Switching Providers

The SDK makes it easy to switch between providers:

OpenAI

final ai = FlutterAI(
  provider: AIProvider.openai,
  config: AIConfig(
    apiKey: 'your-openai-key',
    model: 'gpt-5.5', // or 'gpt-5.4', 'gpt-5.4-mini'
  ),
);

Anthropic (Claude)

final ai = FlutterAI(
  provider: AIProvider.anthropic,
  config: AIConfig(
    apiKey: 'your-anthropic-key',
    model: 'claude-opus-4-8', // or 'claude-opus-4-8', 'claude-haiku-4-5'
  ),
);

Google AI (Gemini)

final ai = FlutterAI(
  provider: AIProvider.googleAI,
  config: AIConfig(
    apiKey: 'your-google-key',
    model: 'gemini-3.5-flash', // or 'gemini-3.5-flash'
  ),
);

Common Patterns

With System Prompt

final ai = FlutterAI(
  provider: AIProvider.anthropic,
  config: AIConfig(
    apiKey: 'your-key',
    systemPrompt: 'You are a coding expert. Provide concise, accurate answers.',
  ),
);

With Token Limits

final ai = FlutterAI(
  provider: AIProvider.openai,
  config: AIConfig(
    apiKey: 'your-key',
    maxTokens: 1000, // Limit response length
    temperature: 0.5, // More focused responses
  ),
);

Error Handling

try {
  final response = await ai.chat('Hello');
  print(response.text);
} on AIAuthenticationError catch (e) {
  print('Check your API key: ${e.message}');
} on AIRateLimitError catch (e) {
  print('Rate limited. Retry after: ${e.retryAfter}');
} on AIError catch (e) {
  print('AI Error: ${e.message}');
}

Next Steps

Now that you have the basics, explore these topics:

Clone this wiki locally