flutter_ai_client 0.2.2 copy "flutter_ai_client: ^0.2.2" to clipboard
flutter_ai_client: ^0.2.2 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.

flutter_ai: a streaming answer with chain-of-thought and a generative-UI task card

flutter_ai_client on pub.dev pub points License: BSD-3-Clause

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's MessageProcessor; notifications are coalesced (injectable scheduler) so high token rates don't drop frames.
  • Full controlsendText, submit, stop, regenerate, clear.
  • Provider/model switchingsetProvider, setOptions, setTools take effect on the next turn without touching the UI.
  • Escape hatch — a raw events stream 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.

☕ Support this project #

Buy me a coffee on Ko-fi

If flutter_ai saves you time, buy me a coffee ☕ — it keeps the whole family maintained.

0
likes
0
points
687
downloads

Publisher

unverified uploader

Weekly Downloads

Provider-agnostic chat controller for flutter_ai: wraps any LlmProvider with optimistic send, cancellation, regeneration, and frame-batched streaming.

Homepage
Repository (GitHub)
View/report issues

Topics

#ai #llm #chat #streaming #flutter

License

unknown (license)

Dependencies

flutter, flutter_ai_core

More

Packages that depend on flutter_ai_client