Skip to content

This tutorial rebuilds the todo app that ships at examples/todo/. Along the way you use records, list state, keyed iteration, a controlled text input, two routed screens, and a capability call. You need a working setup from the first-app tutorial — the CLI built and one host app runnable.

Terminal
flux init todo
cd todo

Replace the contents of main.flux with the data model: a record type and a row component that renders one task.

main.flux
record Task { label: String, done: Bool, }
compo TaskRow(task: Task, tasks: List[Task])
Row gap: 8.0
Text text: task.label
Button text: "Remove", onPress: || { tasks.remove(task) }

A record declares a data type with named fields. TaskRow shows the two ways components receive data: the task it renders comes in as a prop, and the owning list arrives as a second prop so the Remove handler can mutate it.

Add the root component. Its state declarations create signals — the reactive cells that the whole UI reads from.

main.flux
compo TodoApp
state tasks: List[Task] = [
Task(label: "Buy milk", done: false),
Task(label: "Walk dog", done: false),
]
state newTask: String = ""
  • tasks holds the list model. The initial value is a list of record literals.
  • newTask backs the text input. Assigning it in a handler updates every node that reads it — nothing else re-renders.

Wrap the UI in Router / Screen. The host shows only the screen whose route prop matches the active route.

main.flux
compo TodoApp
// ...state declarations from step 2...
Router initialRouteName: "tasks"
Screen route: "tasks"
Column gap: 12.0
Text text: "Flux To-Do"
Row gap: 8.0
TextInput text: $newTask,
onChangeText: fn(v) { newTask = v },
placeholder: "What needs doing?"
Button text: "Add", onPress: || {
tasks.append(Task(label: newTask, done: false))
newTask = ""
}

Three details matter:

  • TextInput is controlled. text: $newTask two-way binds the field to the signal, and onChangeText writes the typed value back. You reset the field by assigning newTask = "" in the Add handler.
  • List mutation is ordinary assignment. tasks.append(...) is the whole update — there is no separate setState.
  • A view call owns an indented child block only when it carries at least one named prop. Router initialRouteName: "tasks" declares initialRouteName, so its Screen children indent beneath it.

Below the input row, iterate over the tasks:

main.flux
compo TodoApp
// ...state, Router, tasks screen from steps 2-3...
Column gap: 8.0
ForEach(tasks, key: fn(t) { t.label }) {
item => TaskRow(task: item, tasks: tasks)
}

The key function identifies each item across edits, so the reconciler reorders and patches existing rows in place — it does not rebuild the list on every change. Because TaskRow receives tasks too, its Remove handler can call tasks.remove(task) and the keyed diff removes exactly that row.

Add a Reset button that clears the list and the persisted copy:

main.flux
compo TodoApp
// ...tasks screen from steps 2-4...
Button text: "Reset", onPress: || {
tasks.clear()
Storage.removeItem(key: "todos")
}

Storage is a capability — a platform service declared in the stdlib and resolved identically on the server and on both hosts. Storage.removeItem (capability 2, method 3) reaches the host, and if the OS permission behind it is denied, the call reports an error instead of crashing.

Finish with the About screen and its navigation handlers:

main.flux
compo TodoApp
// ...tasks screen from steps 2-5...
Button text: "About", onPress: || { RouterNav.navigate("about") }
// Second screen:
Screen route: "about"
Column gap: 12.0
Text text: "About"
Text text: "A real Flux app"
Button text: "Back",
onPress: || { RouterNav.navigate("tasks") }

RouterNav.navigate("about") drives the visible screen. The route prop name is part of the adapter contract — the host reads it by its hashed name, so it must be exactly route.

Start the dev server from the project directory and open the host app:

Terminal
flux dev # WebSocket on :7331

Point the iOS or Android host at ws://127.0.0.1:7331 (see run the host app if you need the platform steps). Add tasks, remove one, switch to the About screen and back, then press Reset.

Generate the native sources for both platforms:

Terminal
flux build ios # -> platforms/ios/Generated/main.swift
flux build android # -> platforms/android/Generated/main.kt

The generated SwiftUI and Compose code renders the same screens you ran in dev — the parity harness proves the two paths stay equivalent.

  • State management — patterns for derived values and shared stores.
  • Navigation — Router, Screen, and RouterNav in depth.
  • Capabilities — the full capability surface and permission handling.