0Pricing
Android Academy · Lesson

Observing Data with Flow

React to database changes reactively.

Observing Data with Flow is a free Android Academy lesson on CoddyKit — lesson 4 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 Android Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Reactive Database Reads

Instead of querying once, Room can return a Flow that emits a new value every time the underlying data changes. Your UI updates automatically.

Returning a Flow from a DAO

Declare the return type as Flow<...>. Notice it is not a suspend function.

@Query("SELECT * FROM users ORDER BY name")
fun observeUsers(): Flow<List<User>>

Why Not suspend Here

A suspend function returns a single result and finishes. A Flow is an ongoing stream, so it is a regular function that returns the stream object; you collect it over time.

How Room Knows to Re-emit

Room tracks which tables a query touches. When any write affects those tables, Room re-runs the query and emits the fresh result down the Flow. This is automatic.

Collecting in a Coroutine

You collect the flow inside a coroutine, typically in the ViewModel.

viewModelScope.launch {
    dao.observeUsers().collect { users ->
        _uiState.value = UiState(users)
    }
}

Mapping the Flow

You can transform the stream with operators like map before exposing it.

val names: Flow<List<String>> =
    dao.observeUsers().map { list ->
        list.map { it.name }
    }

Turning It into StateFlow

Convert a cold DAO flow into a hot StateFlow for the UI with stateIn.

val uiState = dao.observeUsers()
    .map { UiState(it) }
    .stateIn(
        viewModelScope,
        SharingStarted.WhileSubscribed(5000),
        UiState()
    )

Collecting in Compose

The screen collects that state lifecycle-aware, exactly as you learned earlier.

@Composable
fun UsersScreen(vm: UserViewModel) {
    val state by vm.uiState
        .collectAsStateWithLifecycle()
    UserList(state.users)
}

The Full Reactive Chain

Putting it together:

  • Room emits on data change.
  • ViewModel maps to UI state via stateIn.
  • Compose collects with collectAsStateWithLifecycle.

Write to the DB anywhere, and the screen refreshes itself.

Single Source of Truth

This pattern makes the database the single source of truth. The UI never holds stale copies; it always reflects what is actually stored.

Writes Trigger Updates

Any insert, update, or delete on a watched table re-emits the flow, so you do not manually refresh the list after a change.

suspend fun addUser(u: User) = dao.insert(u)
// observeUsers() flow re-emits automatically

Quick Check

Test your understanding of observing data with Flow.

Recap

You learned:

  • DAO methods returning Flow re-emit when the data changes.
  • They are normal (non-suspend) functions returning a stream.
  • Use stateIn + collectAsStateWithLifecycle to drive the UI reactively.
  • The database becomes the single source of truth.

Frequently asked questions

Is the “Observing Data with Flow” lesson free?

Yes — the full text of “Observing Data with Flow” is free to read here on the web, and the Android 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 Android Academy course, upgrade to CoddyKit PRO.

What will I learn in “Observing Data with Flow”?

React to database changes reactively. You practise Android 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 Android Academy?

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

How long does the “Observing Data with 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 Android Academy lesson?

Yes. Every Android 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 and DAOs
  2. The Room Database Class
  3. Queries and Relationships
  4. Observing Data with Flow
← Back to Android Academy