Contents

Generative UI in Flutter with A2UI

Rendering plain Markdown bubbles hits a ceiling quickly in AI-native mobile apps, yet letting an LLM emit raw Dart code for runtime execution is neither safe nor viable in Flutter. Google’s A2UI 1.0 protocol and the official a2ui_flutter package (previously experimental genui) take a declarative middle path: the agent emits schema-bound JSON messages, and the Flutter client validates and renders them using your app’s own trusted native widgets.

Architecture: Declarative Surfaces Over Generated Code

A2UI cleanly separates what an agent wants to present from how your Flutter app renders it:

flowchart LR Agent["Backend Agent (Gemini / Claude / OpenAI)"] -->|"createSurface / updateComponents / updateDataModel"| Client["Flutter Client (a2ui_flutter)"] Client --> Guard["Local Catalog Guardrails (allowedParents & allowedChildren)"] Guard -->|"Valid Tree"| Tree["Trusted Native Flutter Widgets"] Guard -->|"Invalid Nesting"| Reject["Block Malformed Node · Prevent Crash"] Tree -->|"User Action Triggers callAgentFunction"| Agent

Your design system, theming, and accessibility invariants remain completely inside the Flutter codebase, while the agent composes screens by emitting createSurface, updateComponents, and updateDataModel payloads against a registered component catalog.

Dual-Layer Nesting Guardrails: allowedParents and allowedChildren

Unconstrained component nesting is the fastest way for an LLM to trigger Flutter layout exceptions. A2UI 1.0 enforces valid tree structures through two explicit catalog constraints:

  • allowedChildren: Lists which component types a parent component may host.
  • allowedParents: Lists which parent components may contain a given child component.
{
  "Menu": {
    "type": "object",
    "allowedChildren": ["MenuItem"]
  },
  "MenuItem": {
    "type": "object",
    "allowedParents": ["Menu"]
  }
}

These rules operate at two layers:

  1. Prompt guidance: The registered catalog schema is surfaced to the model upfront so it knows valid nesting rules before generating output.
  2. Client enforcement: Before building the widget tree, a2ui_flutter validates constraints locally and blocks malformed blocks if the model hallucinates an invalid hierarchy.

Bidirectional Interactivity with callAgentFunction

Interactive surfaces need to react when users adjust quantities, toggle filters, or submit multi-step forms. callAgentFunction lets generated Flutter widgets invoke deterministic backend agent logic directly:

  • User interactions pass local state changes or explicit intents back to the agent without tearing down and rebuilding the surface.
  • Deterministic work—pricing math, inventory checks, or database queries—runs on the backend, which then streams updateDataModel updates in place while preserving conversational context.

Adding a2ui_flutter and Production Guidelines

Add the official packages to pubspec.yaml (check pub.dev/packages/a2ui_flutter and pub.dev/packages/a2ui_core for current versions):

dependencies:
  flutter:
    sdk: flutter
  a2ui_flutter: ^0.4.0
  a2ui_core: ^0.8.0

Pair this with Flutter Best Practices and Human-Interface Design Best Practices around three practical habits:

  1. Start with a small catalog: Register 8–12 high-frequency components (cards, inputs, pickers, confirmation bars, metric rows) with strict allowedParents/allowedChildren rules before expanding.
  2. Validate side effects server-side: Treat callAgentFunction payloads as untrusted client input—always enforce auth and parameter validation before executing billing, deletes, or writes.
  3. Reserve A2UI for dynamic flows: Keep static high-determinism screens (auth, settings, checkout) as standard Flutter widgets, and use A2UI where layout genuinely adapts to conversation context.

References