0Pricing
Kotlin Multiplatform Academy · Lesson

Expose UI State as StateFlow

Publish a single source of truth for the screen.

Expose UI State as StateFlow is a free Kotlin Multiplatform 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 Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

One Source of Truth

Your screen needs a single, reliable picture of its data. A StateFlow gives you exactly that: one observable value the UI can trust.

Model the Screen as State

Bundle everything the screen shows into one immutable data class. Loading, data and errors all live together in a single object.

data class HomeState(
    val isLoading: Boolean = false,
    val name: String = ""
)

Keep a Private MutableStateFlow

Inside the ViewModel hold a private MutableStateFlow. Only your code may change it, so state stays controlled and predictable.

private val _state =
    MutableStateFlow(HomeState())

Expose a Read-Only View

Publish it as a read-only StateFlow. The UI can collect values but can never write to it directly. Encapsulation, kept clean.

val state: StateFlow<HomeState> =
    _state.asStateFlow()

It Always Has a Value

Unlike a plain Flow, a StateFlow always holds a current value. New collectors instantly get the latest state, no waiting.

Update with .update

Change state safely using update. It copies the current value and applies your change atomically, even across threads.

_state.update { it.copy(isLoading = true) }

copy Keeps It Immutable

Because the state is a data class, copy produces a fresh object with one field changed. Old state is never mutated in place.

Load Data, Then Emit

Launch a coroutine, fetch your data, then emit the new state. The UI re-renders automatically when the value changes. ✨

scope.launch {
    val name = repo.greet()
    _state.update { it.copy(name = name) }
}

StateFlow vs LiveData

Android devs know LiveData, but it is JVM-only. StateFlow works on every KMP target, so it is the portable choice.

Read the Value Directly

Need the current state without collecting? Just read .value. Handy for tests or one-off checks inside the ViewModel.

val current = _state.value

The UI Stays Dumb

The screen only collects state and draws it. All decisions stay in shared code, so both apps behave identically.

Quick Check

Let's confirm why we split mutable and read-only flows.

Recap

Bundle screen data in a state class, hold it in a private MutableStateFlow, and expose a read-only StateFlow the UI just observes. 🚀

Frequently asked questions

Is the “Expose UI State as StateFlow” lesson free?

Yes — the full text of “Expose UI State as StateFlow” is free to read here on the web, and the Kotlin Multiplatform 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 Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Expose UI State as StateFlow”?

Publish a single source of truth for the screen. You practise Kotlin Multiplatform 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 Kotlin Multiplatform Academy?

No prior experience is required. Kotlin Multiplatform 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 “Expose UI State as StateFlow” 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 Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform 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. A Cross-Platform ViewModel Base
  2. Expose UI State as StateFlow
  3. Handle User Intents & Actions
  4. Bind the ViewModel to Each UI
← Back to Kotlin Multiplatform Academy