Android Academy · Lesson

Observing Data with Flow

React to database changes reactively.

Lesson 4 of 413 steps

Observing Data with Flow is a free Android Academy lesson on CoddyKit. This is lesson 4 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

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.
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “Observing Data with Flow” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Observing Data with Flow”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 4 of 4.

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