|
| 1 | +--- |
| 2 | +title: Perplexity Dart & Flutter SDKs |
| 3 | +description: Lightweight, type-safe SDKs for seamless Perplexity API integration in Dart and Flutter applications |
| 4 | +sidebar_position: 3 |
| 5 | +keywords: [Perplexity, Flutter, Dart, SDK, API, streaming, chat-completions, sonar, type-safe, widgets] |
| 6 | +--- |
| 7 | + |
| 8 | +# Perplexity Dart & Flutter SDKs |
| 9 | + |
| 10 | +Comprehensive toolkit for integrating Perplexity's AI capabilities into Dart and Flutter applications. Includes a lightweight core API client (`perplexity_dart`) and Flutter widgets (`perplexity_flutter`) with BLoC state management. |
| 11 | + |
| 12 | +## Features |
| 13 | + |
| 14 | +- **Type-safe API Client** - Fully typed models with compile-time safety |
| 15 | +- **Streaming & Non-streaming** - Real-time chat completions with streaming support |
| 16 | +- **All Perplexity Models** - Sonar, Sonar Pro, Deep Research, Reasoning variants |
| 17 | +- **Multi-Image Processing** - Single or multiple images (base64, data URI, HTTPS URLs) |
| 18 | +- **Flutter Widgets** - Ready-to-use chat UI components with BLoC integration |
| 19 | +- **Advanced Configuration** - Temperature, top-p, search filters, domain restrictions |
| 20 | +- **Cross-platform** - iOS, Android, Web, Desktop support |
| 21 | +- **Future-proof** - Custom model string support for new Perplexity releases |
| 22 | + |
| 23 | +## Prerequisites |
| 24 | + |
| 25 | +- Dart SDK 2.17.0+ |
| 26 | +- Flutter SDK 3.0.0+ (for Flutter features) |
| 27 | +- [Perplexity API Key](https://www.perplexity.ai/settings/api) |
| 28 | + |
| 29 | +## Installation |
| 30 | + |
| 31 | +```bash |
| 32 | +# For Dart projects |
| 33 | +dart pub add perplexity_dart |
| 34 | + |
| 35 | +# For Flutter projects |
| 36 | +flutter pub add perplexity_flutter |
| 37 | +``` |
| 38 | + |
| 39 | +## Usage |
| 40 | + |
| 41 | +### Flutter Widgets (Quick Start) |
| 42 | + |
| 43 | +```dart |
| 44 | +import 'package:flutter/material.dart'; |
| 45 | +import 'package:perplexity_flutter/perplexity_flutter.dart'; |
| 46 | +
|
| 47 | +void main() => runApp(MaterialApp( |
| 48 | + home: ChatWrapperWidget( |
| 49 | + apiKey: 'your-api-key', |
| 50 | + model: PerplexityModel.sonarPro, |
| 51 | + child: Scaffold( |
| 52 | + appBar: AppBar(title: Text('AI Chat')), |
| 53 | + body: Column(children: [ |
| 54 | + Expanded(child: PerplexityChatView()), |
| 55 | + PerplexityChatInput(), |
| 56 | + ]), |
| 57 | + ), |
| 58 | + ), |
| 59 | +)); |
| 60 | +``` |
| 61 | + |
| 62 | +### Core API Client |
| 63 | + |
| 64 | +```dart |
| 65 | +import 'package:perplexity_dart/perplexity_dart.dart'; |
| 66 | +
|
| 67 | +final client = PerplexityClient(apiKey: 'your-api-key'); |
| 68 | +
|
| 69 | +// Simple chat |
| 70 | +final response = await client.sendMessage( |
| 71 | + requestModel: ChatRequestModel( |
| 72 | + model: PerplexityModel.sonarPro, |
| 73 | + messages: [ |
| 74 | + StandardMessageModel(role: MessageRole.user, content: 'Hello AI'), |
| 75 | + ], |
| 76 | + ), |
| 77 | +); |
| 78 | +print('Response: ${response.content}'); |
| 79 | +
|
| 80 | +// Streaming |
| 81 | +final stream = client.streamChat(requestModel: ChatRequestModel( |
| 82 | + model: PerplexityModel.sonar, |
| 83 | + messages: messages, |
| 84 | + stream: true, |
| 85 | +)); |
| 86 | +
|
| 87 | +await for (final chunk in stream) { |
| 88 | + print('Chunk: $chunk'); |
| 89 | +} |
| 90 | +``` |
| 91 | + |
| 92 | +### Image Analysis |
| 93 | + |
| 94 | +```dart |
| 95 | +// Single image |
| 96 | +final request = ChatRequestModel.defaultImageRequest( |
| 97 | + urlList: ['https://example.com/image.jpg'], |
| 98 | + imagePrompt: 'Describe this image', |
| 99 | + model: PerplexityModel.sonarPro, |
| 100 | +); |
| 101 | +
|
| 102 | +// Multiple images |
| 103 | +final multiRequest = ChatRequestModel.defaultImageRequest( |
| 104 | + urlList: [ |
| 105 | + 'https://example.com/photo1.jpg', |
| 106 | + 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...' |
| 107 | + ], |
| 108 | + imagePrompt: 'Compare these images', |
| 109 | +); |
| 110 | +
|
| 111 | +final response = await client.sendMessage(requestModel: request); |
| 112 | +``` |
| 113 | + |
| 114 | +### Advanced Configuration |
| 115 | + |
| 116 | +```dart |
| 117 | +final requestModel = ChatRequestModel( |
| 118 | + model: PerplexityModel.sonar, |
| 119 | + messages: messages, |
| 120 | + temperature: 0.7, // Creativity (0.0-2.0) |
| 121 | + topP: 0.9, // Nucleus sampling |
| 122 | + searchDomainFilter: ['nature.com'], // Limit sources |
| 123 | + returnRelatedQuestions: true, // Follow-up suggestions |
| 124 | + searchRecencyFilter: 'month', // Time filter |
| 125 | + maxTokens: 1000, |
| 126 | +); |
| 127 | +``` |
| 128 | + |
| 129 | +## Code Explanation |
| 130 | + |
| 131 | +### Architecture |
| 132 | + |
| 133 | +**Two-layer design:** |
| 134 | +1. **Core (`perplexity_dart`)** - Pure Dart API client |
| 135 | +2. **UI (`perplexity_flutter`)** - Flutter widgets + BLoC state management |
| 136 | + |
| 137 | +### Multimodal Processing |
| 138 | + |
| 139 | +Uses flexible message parts for combining text and images: |
| 140 | + |
| 141 | +```dart |
| 142 | +final message = ImageMessageModel( |
| 143 | + role: MessageRole.user, |
| 144 | + content: [ |
| 145 | + TextPart(text: 'Analyze these images'), |
| 146 | + ImagePart(url: 'https://example.com/image1.jpg'), |
| 147 | + ImagePart(url: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgA...'), |
| 148 | + ], |
| 149 | +); |
| 150 | +``` |
| 151 | + |
| 152 | +### Available Models |
| 153 | + |
| 154 | +```dart |
| 155 | +PerplexityModel.sonar // 128K tokens - Fast queries |
| 156 | +PerplexityModel.sonarPro // 200K tokens - Enhanced |
| 157 | +PerplexityModel.sonarDeepResearch // 128K tokens - Research |
| 158 | +PerplexityModel.sonarReasoning // 128K tokens - Complex reasoning |
| 159 | +PerplexityModel.sonarReasoningPro // 128K tokens - Advanced reasoning |
| 160 | +``` |
| 161 | + |
| 162 | +### Error Handling |
| 163 | + |
| 164 | +```dart |
| 165 | +try { |
| 166 | + final response = await client.sendMessage(requestModel: request); |
| 167 | +} on PerplexityException catch (e) { |
| 168 | + print('API Error: ${e.message} (${e.statusCode})'); |
| 169 | +} catch (e) { |
| 170 | + print('Error: $e'); |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +## Links |
| 175 | + |
| 176 | +**Packages:** |
| 177 | +- [perplexity_dart on pub.dev](https://pub.dev/packages/perplexity_dart) | [GitHub](https://github.com/vishnu32510/perplexity_dart) |
| 178 | +- [perplexity_flutter on pub.dev](https://pub.dev/packages/perplexity_flutter) | [GitHub](https://github.com/vishnu32510/perplexity_flutter) |
| 179 | + |
| 180 | +**Resources:** |
| 181 | +- [Flutter Examples](https://github.com/vishnu32510/perplexity_dart/tree/main/example_flutter_app) |
| 182 | +- [Dart Examples](https://github.com/vishnu32510/perplexity_dart/tree/main/example) |
| 183 | +- [Perplexity API Docs](https://docs.perplexity.ai/) |
| 184 | + |
| 185 | +**Contributing:** Issues and PRs welcome on both [perplexity_dart](https://github.com/vishnu32510/perplexity_dart/issues) and [perplexity_flutter](https://github.com/vishnu32510/perplexity_flutter/issues) repositories. |
| 186 | + |
| 187 | +## Limitations |
| 188 | + |
| 189 | +- **API Rate Limits** - Implement rate limiting for production use |
| 190 | +- **Image Formats** - Base64, data URI, HTTPS URLs supported; local files need conversion |
| 191 | +- **Context Windows** - 128K-200K token limits per model |
| 192 | +- **API Key Security** - Store securely; avoid hardcoding in client apps |
| 193 | +- **Streaming Dependencies** - Requires WebSocket support on target platforms |
| 194 | +- **Flutter Web** - Limited file operations in web environments |
| 195 | + |
| 196 | +**Production Tips:** Implement API key rotation, retry logic, caching strategies, usage monitoring, and proper error boundaries. |
0 commit comments