StateFlow for Shared UI State
Expose observable state both apps can collect.
StateFlow for Shared UI State 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.
State Needs a Holder
UI state is not a one-off stream, it is a value that always exists right now. StateFlow is a Flow that holds a single current value for the screen.
Always Has a Value
Unlike a plain Flow, a StateFlow is hot and always carries a value. A new collector instantly receives the latest state, never an empty start.
MutableStateFlow to Write
You create state with MutableStateFlow, passing the initial value. This is the writable side your shared logic updates over time.
private val _count = MutableStateFlow(0)Expose a Read-Only View
Keep the mutable field private and expose it as a read-only StateFlow. The UI can observe but never reach in and mutate it directly.
val count: StateFlow<Int> = _count.asStateFlow()Update with .value
Write new state by setting .value on the mutable flow. Every collector is notified of the change right away.
_count.value = _count.value + 1Atomic update {}
For safe read-then-write changes, prefer the update function. It computes the next value from the current one without race conditions.
_count.update { current -> current + 1 }Reads Are Distinct
StateFlow only emits when the value actually changes. Setting the same value again is ignored, so the UI is not woken up for nothing.
A Single Source of Truth
Put your screen state in one StateFlow and both apps read from it. That makes your shared logic the single source of truth for the screen. ✨
Model the Whole Screen
Often the value is a data class describing the entire screen. One UiState object means the UI reads everything it needs from a single place.
data class UiState(
val loading: Boolean = false,
val items: List<String> = emptyList()
)Collect Gets the Latest
When a screen starts collecting, it immediately gets the current state, then every update after. No flicker, no missing first render.
StateFlow vs Flow
Use a plain Flow for events that come and go, and a StateFlow for state that always has a current value. UI almost always wants StateFlow.
Quick Check
One key idea to lock in.
Recap
You used StateFlow as a single source of truth: a hot stream that always holds the screen's current value, ready for both apps to observe. ✨
Frequently asked questions
Is the “StateFlow for Shared UI State” lesson free?
Yes — the full text of “StateFlow for Shared UI State” 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 “StateFlow for Shared UI State”?
Expose observable state both apps can collect. 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 “StateFlow for Shared UI State” 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
- Flow Basics in Shared Code
- StateFlow for Shared UI State
- Operators: map, filter, combine
- Collect Flows on Android & iOS