Tutorial 4
Fetch data without freezing the app.
Network data is unreliable by default. A junior Android app must handle loading, success, empty, and error states explicitly.
Network states
Loading
Success
Empty
Error + retry
DTO and mapper
data class BusinessDto(
val id: Int,
val name: String,
val category: String,
val city: String,
val phone: String?
)
data class Business(
val id: Int,
val name: String,
val category: String,
val city: String,
val phone: String
)
fun BusinessDto.toBusiness(): Business {
return Business(
id = id,
name = name.trim(),
category = category,
city = city,
phone = phone ?: "Not available"
)
}Keep DTOs separate from UI models. Network payloads often contain nulls, naming mismatches, or fields your UI should not trust directly.
Practice task
- Create DTO and UI model classes.
- Map nullable API fields into safe UI values.
- Show loading, success, empty, and error screens.
- Add a Retry button that repeats the request.