0Pricing
Kotlin Academy · Lesson

Modeling UI State with Sealed Classes

Apply sealed classes to represent Loading, Success, and Error UI states.

The UI State Problem

UI screens have multiple states: loading, showing data, showing an error, or empty. Managing these with booleans (isLoading, hasError) is fragile. Sealed classes fix this.

The Classic Boolean Hell

Using separate booleans leads to impossible states.
// Fragile: isLoading=true AND hasError=true is invalid
var isLoading = false
var hasError = false
var data: List<Item>? = null
// 8 combinations, most of which are invalid!

All lessons in this course

  1. sealed class vs sealed interface: When to Use Each
  2. Exhaustive when with Sealed Hierarchies
  3. Modeling UI State with Sealed Classes
  4. Nesting and Combining Sealed Hierarchies
← Back to Kotlin Academy