0Pricing
Jetpack Compose Academy · Lesson

Modeling a UiState Data Class

Represent the screen with one state object.

Modeling a UiState Data Class is a free Jetpack Compose Academy lesson on CoddyKit — lesson 3 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 Jetpack Compose Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.

Frequently asked questions

Is the “Modeling a UiState Data Class” lesson free?

Yes — the full text of “Modeling a UiState Data Class” is free to read here on the web, and the Jetpack Compose 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 Jetpack Compose Academy course, upgrade to CoddyKit PRO.

What will I learn in “Modeling a UiState Data Class”?

Represent the screen with one state object. You practise Jetpack Compose 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 Jetpack Compose Academy?

No prior experience is required. Jetpack Compose Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Modeling a UiState Data Class” 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 Jetpack Compose Academy lesson?

Yes. Every Jetpack Compose 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. Why ViewModel Survives Rotation
  2. viewModel() in a Composable
  3. Modeling a UiState Data Class
  4. collectAsStateWithLifecycle
← Back to Jetpack Compose Academy