<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Anant Mandanka's Blog]]></title><description><![CDATA[Anant Mandanka's Blog]]></description><link>https://anant8140.hashnode.dev</link><generator>RSS for Node</generator><lastBuildDate>Fri, 04 Dec 2026 22:50:27 GMT</lastBuildDate><atom:link href="https://anant8140.hashnode.dev/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[Streaming ChatGPT and Gemini replies in Flutter without the flicker]]></title><description><![CDATA[If you have built an AI chat screen in Flutter, you have probably seen this: the reply streams in, and the screen fights it. Raw ** shows up until the…]]></description><link>https://anant8140.hashnode.dev/streaming-chatgpt-and-gemini-replies-in-flutter-without-the-flicker</link><guid isPermaLink="true">https://anant8140.hashnode.dev/streaming-chatgpt-and-gemini-replies-in-flutter-without-the-flicker</guid><category><![CDATA[Flutter]]></category><category><![CDATA[Dart]]></category><category><![CDATA[AI]]></category><dc:creator><![CDATA[Anant mandanka]]></dc:creator><pubDate>Tue, 01 Dec 2026 07:17:13 GMT</pubDate><content:encoded><![CDATA[<p>If you have built an AI chat screen in Flutter, you have probably seen this: the reply streams in, and the screen fights it. Raw <code>**</code> shows up until the closing pair arrives, a code block jumps when the highlighter kicks in, and long answers get slower with every chunk.</p>
<p>I hit all of that while building chat screens, so I wrote <a href="https://pub.dev/packages/llm_markdown">llm_markdown</a>, a Markdown widget made for replies that are still being written. This post shows why the usual approach struggles and how to stream a reply smoothly.</p>
<p><img src="https://raw.githubusercontent.com/anant8140/llm_markdown/main/doc/demo.gif" alt="A reply streaming in: thinking, an alert, code, a diagram, math, a table" /></p>
<p><strong><a href="https://anant8140.github.io/llm_markdown/">Try the live demo in your browser</a></strong></p>
<h2>Why the usual way flickers</h2>
<p>The common setup is a regular Markdown widget rebuilt with the whole text on every chunk:</p>
<pre><code class="language-dart">MarkdownBody(data: textSoFar) // rebuilt for every chunk
</code></pre>
<p>That has three problems while a reply streams:</p>
<ol>
<li><strong>Everything is parsed again on every chunk.</strong> A 20,000 character answer arrives in a few thousand chunks, so the work per chunk keeps growing with the reply.</li>
<li><strong>Half-typed syntax shows raw.</strong> Until the model sends the closing <code>**</code> or the closing code fence, the reader sees the markers.</li>
<li><strong>Text arrives in bursts.</strong> Models send uneven chunks, so the reply jumps instead of flowing.</li>
</ol>
<h2>What llm_markdown does instead</h2>
<ul>
<li><strong>Only the last, unfinished block is parsed again.</strong> Finished blocks keep their widgets.</li>
<li><strong>Half-typed Markdown is closed before rendering</strong>, so a half-open bold, code fence or link already looks right.</li>
<li><strong>Text is revealed at a steady pace</strong> that speeds up when text is waiting, so the view never falls far behind.</li>
</ul>
<p>On top of that it draws code highlighting, tables, math, Mermaid diagrams (natively, no WebView), alerts, checklists, thinking sections and <code>[1]</code> citations.</p>
<h2>How much faster</h2>
<p>I streamed the same 20,000 character reply (headings, lists, code blocks, tables) in 2,472 chunks into both widgets, one frame per chunk, three runs each:</p>
<table>
<thead>
<tr>
<th></th>
<th>memory peak</th>
<th>total time</th>
<th>95% of chunks under</th>
</tr>
</thead>
<tbody><tr>
<td>llm_markdown</td>
<td>+123 to +131 MB</td>
<td>11 to 12 s</td>
<td>about 8 ms</td>
</tr>
<tr>
<td>flutter_markdown_plus</td>
<td>+282 to +294 MB</td>
<td>145 to 148 s</td>
<td>about 138 ms</td>
</tr>
</tbody></table>
<p>So about 2.3 times less memory and 12 times less time. A typical chunk took about 8 ms instead of about 138 ms, which is the difference between smooth and a visible stutter. It was a debug build, so compare the rows rather than the absolute numbers. The <a href="https://github.com/anant8140/llm_markdown/tree/main/benchmark">benchmark and how to run it</a> are in the repo.</p>
<h2>Using it</h2>
<pre><code class="language-bash">flutter pub add llm_markdown
</code></pre>
<p>Keep one controller per reply and append chunks as they arrive:</p>
<pre><code class="language-dart">final controller = StreamingMarkdownController();

// in build()
StreamingMarkdown(controller: controller)

// when the reply comes in
await for (final chunk in model.generate(prompt)) {
  controller.append(chunk);
}
controller.complete();
</code></pre>
<p><code>controller.addStream(stream)</code> does that loop for you. If your state already keeps the text, this works too, and only the new part is processed:</p>
<pre><code class="language-dart">StreamingMarkdown.text(message.text, isComplete: message.done)
</code></pre>
<p>There are one-line helpers for OpenAI, Anthropic and Gemini streams, and a <a href="https://pub.dev/packages/llm_markdown_firebase_ai">Firebase AI adapter</a> that also fits the <code>responseBuilder</code> of the Flutter AI Toolkit.</p>
<h2>A whole chat screen</h2>
<p>If you want the screen around the replies too, <a href="https://pub.dev/packages/llm_markdown_chat">llm_markdown_chat</a> gives you the message list, the input box, send and stop, copy and retry, for any model:</p>
<pre><code class="language-dart">final chat = LlmChatController(
  onReply: (prompt, reply, history) async {
    final response = await client.send(buildRequest(history, prompt));
    await reply.addOpenAIStream(response.stream);
  },
);

LlmChatView(controller: chat)
</code></pre>
<p><img src="https://raw.githubusercontent.com/anant8140/llm_markdown/main/packages/llm_markdown_chat/doc/chat.gif" alt="The llm_markdown_chat screen" /></p>
<p><a href="https://anant8140.github.io/llm_markdown/chat/">Chat demo in the browser</a>. There is also a ready <a href="https://github.com/anant8140/flutter_ai_chat_template">ChatGPT-style app template</a> built on it, for OpenAI, Claude, Gemini and Ollama.</p>
<h2>Replies that are read aloud</h2>
<p>A reader on Reddit asked for this one: in a voice app, the screen should never show text the voice has not said yet. Version 0.7.1 added <code>revealLimit</code>. The whole reply streams and is parsed as usual, but the screen shows text only up to the position you give it:</p>
<pre><code class="language-dart">final spoken = ValueNotifier(0);

StreamingMarkdown(
  controller: controller,
  revealLimit: voiceOn ? spoken : null, // null: shown as usual
);

// from your text-to-speech progress
spoken.value = endOfSentenceBeingSpoken;
</code></pre>
<p>If a reply is pulled while it is being spoken, <code>controller.reset()</code> clears it in one frame, cached blocks included. The spoken position belongs to your app, so set <code>spoken.value = 0</code> before the next reply starts.</p>
<h2>Try it</h2>
<ul>
<li>Package: <a href="https://pub.dev/packages/llm_markdown">pub.dev/packages/llm_markdown</a></li>
<li>Code: <a href="https://github.com/anant8140/llm_markdown">github.com/anant8140/llm_markdown</a></li>
<li>Live demo: <a href="https://anant8140.github.io/llm_markdown/">anant8140.github.io/llm_markdown</a></li>
</ul>
<p>It has no Material or Cupertino dependency, and every color, font, label and builder can be changed. Bug reports and ideas are very welcome. What do you use today for streaming replies, and what bothers you about it?</p>
]]></content:encoded></item></channel></rss>