flutter_ai_client 0.3.0
flutter_ai_client: ^0.3.0 copied to clipboard
Provider-agnostic chat controller for flutter_ai: wraps any LlmProvider with optimistic send, cancellation, regeneration, and frame-batched streaming.
flutter_ai_client
The useChat controller for Flutter — wrap any LlmProvider and get optimistic send, batched streaming, cancel, and regenerate as a plain Listenable. No state-manager lock-in.
Family: flutter_ai ·
core · elements ·
openai · anthropic · gemini ·
tools · mcp · voice
Recipes · Migrating from the Vercel AI SDK
The transcript above is driven by this package's UseChatController (rendered with flutter_ai_elements).
Provider-agnostic chat controller for the flutter_ai family.
UseChatController wraps any LlmProvider (from flutter_ai_core) and exposes
conversation state as a plain Listenable — so it drops into ListenableBuilder
and adapts cleanly to Bloc, Riverpod, or Provider. It bundles no state-manager
of its own.
Features #
- Optimistic send — the user's message paints synchronously, before the request is dispatched.
- Streaming, batched — events are folded by
flutter_ai_core'sMessageProcessor; notifications are coalesced (injectable scheduler) so high token rates don't drop frames. - Full control —
sendText,submit,stop,regenerate,clear. - Provider/model switching —
setProvider,setOptions,setToolstake effect on the next turn without touching the UI. - Escape hatch — a raw
eventsstream for custom state layers.
Usage #
final controller = UseChatController(
provider: myProvider, // any LlmProvider
options: const AiRequestOptions(model: 'gpt-4o'),
);
// Bind to the UI — rebuilds when the conversation changes.
ListenableBuilder(
listenable: controller,
builder: (context, _) => ListView(
children: [
for (final m in controller.messages) Text('${m.role.name}: ${m.text}'),
],
),
);
// Send / stop.
controller.sendText('Hello');
if (controller.status.isBusy) controller.stop();
// Switch model live.
controller.setOptions(const AiRequestOptions(model: 'gpt-4o-mini'));
See example/ for a minimal end-to-end widget.
Implementing a provider #
A provider maps your backend's stream onto flutter_ai_core's AiStreamEvents:
class MyProvider implements LlmProvider {
@override
Stream<AiStreamEvent> send(
AiConversation conversation, {
List<ToolDefinition>? tools,
AiRequestOptions? options,
}) async* {
yield const MessageStarted(messageId: 'a1', role: AiRole.assistant);
yield const TextDelta(messageId: 'a1', delta: 'Hello!');
yield const MessageFinished(messageId: 'a1', reason: FinishReason.stop);
}
}
Status #
Published on pub.dev (see the CHANGELOG for versions); depends on flutter_ai_core.
If flutter_ai saves you time, you can buy me a coffee ☕.