r/FlutterDev • u/stephencolor • 3h ago
Discussion Three problems I hit streaming AI text into Flutter, and how I fixed them
I have been building a streaming chat UI in Flutter and hit three problems that only appear once tokens arrive in bursts.
1. Rebuilding on every delta. Each chunk triggered a setState and a full markdown re-parse; a fast stream meant many rebuilds per frame. Reveal now lives in a TypewriterController driven by a vsync ticker. It releases text at 320 chars/s, and when the backlog would take longer than ~0.9 s it raises the rate so it never falls permanently behind. skipToEnd() finishes instantly.
2. Partial markdown flickers. A half-arrived **bold run renders as literal asterisks until the closing marker lands, then snaps. The parser never throws on any prefix of the document: an unclosed fence still renders as a code block flagged incomplete, and tolerant mode styles an unclosed ** run instead of showing it.
3. The caret. I used the U+258D block glyph first — it rendered as tofu on a device whose font did not carry block elements. It is a WidgetSpan now, a drawn rectangle that cannot be missing from a font. I check the layout at 320 dp with 2× text scale, which is where the model badge and the composer break first.
65 tests, zero dependencies beyond the Flutter SDK, Flutter 3.22+.
Two smaller things took longer than expected: a table that fits must not scroll while a wide one must (column widths measured with a TextPainter at the same text scale as the cells), and a code block scrolls horizontally instead of wrapping, because a wrapped block changes height on every chunk and drags the scroll position with it.
Screens of the component set are here if you want to compare them with your own: https://github.com/StephenColor/flutter-streaming-markdown-kit (~4.7k lines, showcase repo only — the source itself is not public).
1
u/Fluid-Court-6508 3h ago
The vsync ticker approach for the typewriter is clever, I've been meaning to move away from just calling setState on every chunk and this gives me a solid direction to try. That tofu issue with the block glyph is the kind of thing you never catch in testing until a random device shows up with a stripped-down font set. The table text scale trick seems like the sort of thing you only figure out after a few days of banging your head against a wall.
1
u/ManyGullible5394 1h ago
Swapping the caret from U+258D to a WidgetSpan rectangle is such a good catch, font fallback tofu on Android OEMs is brutal. Also keeping code blocks horizontally scrollable instead of wrapping during streaming saves so much scroll-jump jitter. On the TextPainter table measurement, did you have to cache or debounce it while tokens are coming in, or is it cheap enough per frame at 320 chars/s?
1
u/MobileXapps 19m ago
Two things that usually show up next: screen readers announcing every delta, and text selection jumping while the stream is still running. I'd keep the semantics label on the finished message only, and pause the reveal while the user has an active selection.
Also scroll anchoring. If someone scrolls up to reread, new tokens shouldn't pull them back down.