Quick start
A shopping list that an agent fills, asks before it clears and draws on, in one file.
Requires Dart 3.13 (Flutter 3.47) or newer. The example is a shopping list on a Cubit. Its five Dart blocks make one file, quick_start..
Add the packages
Keep tandem and tandem_generator on the same minor version, because the generated code follows the API of tandem.
flutter pub add tandem flutter_bloc
flutter pub add dev:build_runner dev:tandem_generatorList what the agent may see and call
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:tandem/tandem.dart';
part 'quick_start.g.dart';
@TandemAgent()
class ShoppingCubit() extends Cubit<List<String>> with _$ShoppingCubit {
this : super(const []);
@override
List<AgentCapability> get agentCapabilities => [
.sees(items),
.calls(add),
.callsWithApproval(clear),
];
@override
Stream<Object?> get changes => stream;
/// What is on the shopping list now.
List<String> get items => state;
/// Adds [item] to the list.
void add(String item) => emit([...state, item]);
/// Removes every item from the list.
void clear() => emit(const []);
}Doc comments describe the listed data and methods to the agent, and parameter types define the tools' schemas. Tandem reads the list before each run and call, so it can depend on the state or the current user.
changes reports every state change. A Cubit returns its stream. Any state library covers a ChangeNotifier and Riverpod.
Wrap the screen
final class const ShoppingScreen({super.key}) extends StatelessWidget {
@override
Widget build(BuildContext context) => BlocProvider(
create: (context) => ShoppingCubit(),
child: TandemScope(
surface: (context) => context.read<ShoppingCubit>(),
approvals: const .callback(confirmClear),
child: Scaffold(
body: BlocBuilder<ShoppingCubit, List<String>>(
builder: (context, items) => SingleChildScrollView(
child: Column(
children: [
for (final item in items) ListTile(title: Text(item)),
const TandemView(catalog: ShoppingCatalog()),
],
),
),
),
),
),
);
}The scope reads the Cubit through surface when the session opens. approvals names the function that asks the user. The agent draws its views where the TandemView is.
Ask the user before the list is cleared
Future<void> confirmClear(
BuildContext context,
ApprovalRequest request,
TandemSession session,
) async {
final approved =
await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title: const Text('Clear the list?'),
actions: [
TextButton(
onPressed: () => Navigator.pop(context, false),
child: const Text('Cancel'),
),
FilledButton(
onPressed: () => Navigator.pop(context, true),
child: const Text('Clear'),
),
],
),
) ??
false;
session.decide(request, approved: approved);
}clear is listed with ., so Tandem calls confirmClear before the method runs. A change of items while the dialog is open invalidates the request, and decide then returns false instead of deciding. Approvals explains how the decision stays tied to the screen's data.
List the widgets the agent may draw with
/// Suggested items, one below another.
final class const Suggestions({super.key, required final List<Widget> children})
extends StatelessWidget {
@override
Widget build(BuildContext context) => Column(children: children);
}
/// An item to add to the list, with a button that adds it.
final class const Suggestion({
super.key,
required final String item,
required final VoidCallback onAdd,
}) extends StatelessWidget {
@override
Widget build(BuildContext context) => ListTile(
title: Text(item),
trailing: TextButton(onPressed: onAdd, child: const Text('Add')),
);
}
/// Suggest items the list does not have yet. Draw one Suggestions with a
/// Suggestion for each item. Its onAdd calls add with that item.
///
/// ```json
/// [
/// {"id": "root", "component": "Suggestions", "children": ["milk"]},
/// {"id": "milk", "component": "Suggestion", "item": "milk",
/// "onAdd": {"action": {"event": {"name": "add", "context": {"item": "milk"}}}}}
/// ]
/// ```
@TandemCatalog([Suggestions, Suggestion])
final class const ShoppingCatalog() with _$ShoppingCatalog;The agent reads the catalog's doc comment. Its json block is one example view, which every catalog needs. The build checks the example's component and property names. The button of a Suggestion calls add, with the same checks as the agent's own call. GenUI builds a larger catalog step by step.
Enable the debug bridge
void main() {
TandemBridge.enable();
runApp(const MaterialApp(home: ShoppingScreen()));
}TandemBridge exists only in a debug build. Your coding agent explains how the agent reaches the app through it.
Generate the code
dart run build_runner buildEvery class with @TandemAgent() or @TandemCatalog gets a _$Name mixin. The list can depend on the state through if, switch or a spread. The mixin copies it and names each . by its getter, so two values of the same type stay apart. Write every . in agentCapabilities itself, rather than in a helper it calls, and keep super out of the getter.
An outdated . is not always caught at runtime, so check the generated code in CI:
dart run build_runner build && git diff --exit-codeRun it with your coding agent
Install the package's skills and run the app in debug. The skill tandem-coding-agent teaches your coding agent the bridge.
dart run skills@ get --package tandem --all
flutter runAsk your coding agent to add milk and eggs to the list. Ask it to clear the list, and the dialog asks you first. Ask it to draw what pancakes need, and it draws a Suggestion for each item.
An in-app agent connects the same screen to an agent backend or to Firebase AI Logic, with a chat for your users.