Memodelkan Data Class UiState
Representasikan layar dengan satu objek state.
Memodelkan Data Class UiState adalah pelajaran Jetpack Compose Academy gratis di CoddyKit. Ini adalah pelajaran 3 dari 4. Kamu bisa membaca pelajaran lengkapnya di bawah secara gratis — lalu praktikkan langsung di browser dengan editor kode bawaan dan tutor AI 24/7. Ini adalah bagian dari jalur belajar Jetpack Compose Academy, dan progresmu tersinkronisasi di web dan aplikasi CoddyKit. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.
Bagian dari pelajaran ini belum diterjemahkan dan ditampilkan dalam bahasa Inggris.
One Object for the Screen
Instead of juggling many separate fields, model the whole screen with a single UiState data class that describes exactly what to draw.
data class ProfileUiState(
val name: String = "",
val isLoading: Boolean = false
)Why One State Object
Grouping fields together prevents impossible combinations and makes the UI a pure function of one value: same state in, same screen out.
Make It Immutable
Use val properties so a UiState can never be mutated in place. You always produce a new state, which keeps Compose updates predictable.
Updating With copy
Data classes give you copy() for free, so you can build the next state by changing only the fields that differ.
val next = state.copy(isLoading = false)Modeling Loading
Capture progress with a flag like isLoading so the UI can show a spinner while work runs, all from the same state object.
Modeling Errors
Add a nullable errorMessage field. When it is non-null, the screen shows the error; when null, everything is fine. ⚠️
data class UiState(
val items: List<Item> = emptyList(),
val errorMessage: String? = null
)Sealed Classes for Phases
For mutually exclusive phases, a sealed hierarchy is cleaner: Loading, Success, and Error become distinct types you handle exhaustively.
sealed interface UiState {
object Loading : UiState
data class Success(val data: List<Item>) : UiState
}Sensible Defaults
Give every property a default value so the initial state is one easy expression and new screens start in a clean, known condition.
private val _state = MutableStateFlow(ProfileUiState())Exposing It Read-Only
Hold a mutable state inside the ViewModel but expose it as a read-only type, so only the ViewModel may change the screen’s state.
val state: StateFlow<ProfileUiState> = _state.asStateFlow()Rendering From It
Your Composable reads one UiState and branches on its fields. The render logic stays tiny because the state already says what to show.
Single Source of Truth
A UiState class becomes the single source of truth for the screen, so your UI and logic can never quietly drift out of sync. ✨
Quick Check
Test your modeling instincts for screen state.
Recap
Model the screen with one immutable UiState data class, update it with copy(), expose it read-only, and let the UI render purely from that single source.
Pertanyaan yang Sering Diajukan
Apakah pelajaran “Memodelkan Data Class UiState” gratis?
Ya — teks lengkap “Memodelkan Data Class UiState” gratis dibaca di sini di web. Untuk praktiknya secara interaktif (editor kode bawaan dan tutor AI 24/7) dan buka sisa kursus Jetpack Compose Academy, upgrade ke CoddyKit PRO. Kursus Jetpack Compose Academy mencakup 4 pelajaran total.
Apa yang akan aku pelajari di “Memodelkan Data Class UiState”?
Representasikan layar dengan satu objek state. Kamu berlatih Jetpack Compose Academy dengan kode praktik yang langsung kamu jalankan di browser, dan tutor AI 24/7 menjawab pertanyaanmu saat kamu mengerjakan pelajaran ini.
Apakah aku perlu pengalaman untuk memulai Jetpack Compose Academy?
Tidak diperlukan pengalaman sebelumnya. Jetpack Compose Academy di CoddyKit dirancang untuk pemula hingga pelajar tingkat lanjut, jadi kamu bisa memulai di sini atau dari awal dan belajar sesuai kecepatan kamu sendiri. Ini adalah pelajaran 3 dari 4.
Berapa lama pelajaran “Memodelkan Data Class UiState” memakan waktu?
Sebagian besar pelajaran CoddyKit memakan waktu sekitar 5–10 menit. Setiap pelajaran ringkas dan interaktif, jadi kamu membuat kemajuan stabil dan melanjutkan dari tempat kamu tinggalkan di web dan aplikasi.
Bisakah aku menulis dan menjalankan kode dalam pelajaran Jetpack Compose Academy ini?
Ya. Setiap pelajaran Jetpack Compose Academy menyertakan editor kode bawaan, jadi kamu menulis dan menjalankan kode nyata langsung di browser dan mendapatkan umpan balik AI instan — tidak diperlukan penyiapan lokal.
Semua pelajaran dalam kursus ini
- Mengapa ViewModel Bertahan setelah Rotasi
- viewModel() dalam Composable
- Memodelkan Data Class UiState
- collectAsStateWithLifecycle