将界面状态公开为 StateFlow
为界面发布唯一真实数据源
将界面状态公开为 StateFlow 是 CoddyKit 上的免费 Kotlin Multiplatform Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Kotlin Multiplatform Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Kotlin Multiplatform Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
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. 🚀
常见问题解答
「将界面状态公开为 StateFlow」课时是免费的吗?
是的 — 「将界面状态公开为 StateFlow」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Kotlin Multiplatform Academy 课程的其余内容,请升级到 CoddyKit PRO。 Kotlin Multiplatform Academy 课程共包含 4 节课。
「将界面状态公开为 StateFlow」这节课中我会学到什么?
为界面发布唯一真实数据源 你通过在浏览器中直接运行的动手代码来练习 Kotlin Multiplatform Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Kotlin Multiplatform Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Kotlin Multiplatform Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「将界面状态公开为 StateFlow」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Kotlin Multiplatform Academy 课中编写并运行代码吗?
能。每节 Kotlin Multiplatform Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 跨平台 ViewModel 基类
- 将界面状态公开为 StateFlow
- 处理用户意图与操作
- 将 ViewModel 绑定到各个界面