0Pricing
Jetpack Compose Academy · Lesson

Reactive Queries Returning Flow

Observe the database in Compose.

Reactive Queries Returning Flow is a free Jetpack Compose Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Static Reads Go Stale

A one-time query gives you data once, but the table keeps changing. You want the UI to refresh whenever rows are added or removed.

Return a Flow Instead

Change a DAO query to return a Flow. Room now emits a fresh list every single time the underlying table changes. 🔄

@Query("SELECT * FROM Note")
fun observeAll(): Flow<List<Note>>

No suspend on Flow Queries

A Flow query is not marked suspend. It returns the stream immediately, and emissions arrive as the data updates over time.

Room Watches the Tables

Behind the scenes Room tracks which tables a query touches. When any of them changes, it re-runs the query and emits a new value.

Collect the Flow Safely

In your ViewModel, collect the Flow inside a coroutine. Use viewModelScope so collection stops when the ViewModel clears.

viewModelScope.launch {
    dao.observeAll().collect { notes -> /* update state */ }
}

Turn Flow Into State

Convert the stream into observable UI state with stateIn, giving an initial value so the screen has something to show at once.

val notes = dao.observeAll().stateIn(viewModelScope, SharingStarted.Eagerly, emptyList())

Observe It in Compose

In the Composable read the StateFlow with collectAsStateWithLifecycle. The list recomposes automatically on every change.

val notes by viewModel.notes.collectAsStateWithLifecycle()

Insert and Watch It Appear

Insert a row anywhere and the Flow emits again, so the new item shows up instantly without you refreshing manually.

Map Inside the Flow

You can transform results with map before they reach the UI, like sorting or filtering, all reactively in the stream.

dao.observeAll().map { it.sortedBy(Note::text) }

Flows Beat LiveData Here

Flow integrates cleanly with coroutines and Compose, making it the modern choice over LiveData for reactive Room queries.

One Source, Always Current

With reactive queries the database becomes your single source of truth, and the UI is just a reflection of it at every moment.

Quick Check

What happens when a Room DAO query returns a Flow?

Recap: Reactive Reads

You made queries return Flow, collected them into state, and watched Compose redraw on every change. Next you will organize this behind a repository.

Frequently asked questions

Is the “Reactive Queries Returning Flow” lesson free?

Yes — the full text of “Reactive Queries Returning Flow” is free to read here on the web, and the Jetpack Compose Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reactive Queries Returning Flow”?

Observe the database in Compose. You practise Jetpack Compose Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reactive Queries Returning Flow” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Entities, DAOs & the Database
  2. Reactive Queries Returning Flow
  3. The Repository Pattern
  4. Offline-First Caching Strategy
← Back to Jetpack Compose Academy