UI-Zustand als StateFlow bereitstellen
Eine zentrale Quelle der Wahrheit für den Screen veröffentlichen
UI-Zustand als StateFlow bereitstellen ist eine kostenlose Kotlin Multiplatform Academy-Lektion auf CoddyKit. Dies ist Lektion 2 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Kotlin Multiplatform Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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.valueThe 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. 🚀
Häufig gestellte Fragen
Ist die Lektion „UI-Zustand als StateFlow bereitstellen“ kostenlos?
Ja — der vollständige Text von „UI-Zustand als StateFlow bereitstellen“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Kotlin Multiplatform Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Kotlin Multiplatform Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „UI-Zustand als StateFlow bereitstellen“?
Eine zentrale Quelle der Wahrheit für den Screen veröffentlichen Du übst Kotlin Multiplatform Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Kotlin Multiplatform Academy zu starten?
Keine Vorkenntnisse erforderlich. Kotlin Multiplatform Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 2 von 4.
Wie lange dauert die Lektion „UI-Zustand als StateFlow bereitstellen“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Kotlin Multiplatform Academy-Lektion Code schreiben und ausführen?
Ja. Jede Kotlin Multiplatform Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Eine plattformübergreifende ViewModel-Basisklasse
- UI-Zustand als StateFlow bereitstellen
- User-Intents und Aktionen verarbeiten
- Das ViewModel an jede UI binden