0Pricing
Jetpack Compose Academy · レッスン

UiStateデータクラスをモデル化する

1つの状態オブジェクトで画面を表現します。

「UiStateデータクラスをモデル化する」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

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.

よくある質問

「UiStateデータクラスをモデル化する」レッスンは無料ですか?

はい。「UiStateデータクラスをモデル化する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。

「UiStateデータクラスをモデル化する」で何を学びますか?

1つの状態オブジェクトで画面を表現します。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Jetpack Compose Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「UiStateデータクラスをモデル化する」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このJetpack Compose Academyレッスンでコードを書いて実行できますか?

はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. ViewModelが回転後も保持される理由
  2. Composable内のviewModel()
  3. UiStateデータクラスをモデル化する
  4. collectAsStateWithLifecycle
← Jetpack Compose Academyに戻る