0Pricing
Kotlin Multiplatform Academy · Lesson

Expose Results as Flow

Stream repository data to both UIs reactively.

Expose Results as Flow is a free Kotlin Multiplatform Academy lesson on CoddyKit — lesson 4 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 Kotlin Multiplatform Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Beyond One-Shot Calls

A suspend function returns once, but real screens want updates over time. That is exactly what Flow gives you.

Return a Flow

Change the repository to return a Flow of your domain type, so callers can react to a stream of results.

fun observeUsers(): Flow<List<User>>

Emit With flow Builder

Build a stream using the flow builder and call emit to push each new value to whoever is collecting.

fun observeUsers() = flow { emit(getUsers()) }

Suspend Inside Flow

Inside the builder you can freely call suspend functions, so your network fetch fits right in.

Combine With Local Data

A Flow can emit cached data first, then a fresh network result, giving the UI an instant view that updates.

flow {
    emit(loadCached())
    emit(fetchFresh())
}

Map the Stream

Use the map operator on the Flow to transform each emission before the UI ever sees it.

observeUsers().map { it.sortedBy(User::name) }

Catch Errors in the Flow

Wrap the stream with catch so a network failure becomes a graceful emission instead of a crash.

observeUsers().catch { emit(emptyList()) }

Collect on Each Platform

Both apps collect the Flow: Compose with collectAsState, and SwiftUI through a small bridge.

scope.launch { repo.observeUsers().collect { render(it) } }

Still Shared, Still Clean

The Flow logic lives in commonMain, so both platforms share one reactive data source with no copies.

A Single Source of Truth

Exposing data as a Flow gives the whole app one source of truth that everyone observes and trusts.

The Reactive Repository

Here is the repository as a stream: it emits fresh data and never lets a failure break the collector.

fun observeUsers(): Flow<List<User>> = flow {
    emit(api.getUsers().toDomain())
}.catch { emit(emptyList()) }

Quick Check

Why expose repository data as a Flow instead of a single suspend return?

Recap

Returning a Flow turns the repository into a reactive source: it emits updates over time, both apps collect it, and errors stay contained.

Frequently asked questions

Is the “Expose Results as Flow” lesson free?

Yes — the full text of “Expose Results as Flow” is free to read here on the web, and the Kotlin Multiplatform 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 Kotlin Multiplatform Academy course, upgrade to CoddyKit PRO.

What will I learn in “Expose Results as Flow”?

Stream repository data to both UIs reactively. You practise Kotlin Multiplatform 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 Kotlin Multiplatform Academy?

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

How long does the “Expose Results as Flow” 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 Kotlin Multiplatform Academy lesson?

Yes. Every Kotlin Multiplatform 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. Define a Repository Interface
  2. Wrap the API Behind the Repository
  3. Map DTOs to Domain Models
  4. Expose Results as Flow
← Back to Kotlin Multiplatform Academy