Android Academy · Lesson

Kotlin Flow Basics

Emit and collect streams of data.

Lesson 3 of 413 steps

Kotlin Flow Basics is a free Android Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Android Academy learning path, and your progress syncs across the web and the CoddyKit app. The Android Academy course includes 4 lessons in total.

What Is a Flow?

A Flow is an asynchronous stream that emits multiple values over time. Think of it as a suspendable sequence you can transform and collect.

Building a Flow

The flow { } builder lets you emit values, optionally suspending between them.

fun numbers(): Flow<Int> = flow {
    for (i in 1..3) {
        delay(100)
        emit(i)
    }
}

Collecting a Flow

A flow does nothing until you collect it. Collection is a suspend operation, so it runs in a coroutine.

scope.launch {
    numbers().collect { value ->
        println(value)
    }
}

Cold Streams

Flows are cold: the builder block runs fresh for each collector. Two collectors each get their own independent sequence from the start.

Transforming with map and filter

Operators transform emissions lazily, only running when collected.

numbers()
    .map { it * 10 }
    .filter { it > 10 }
    .collect { println(it) }

More Operators

  • take(n) — only the first n values.
  • onEach — side effect per value.
  • distinctUntilChanged — skip repeats.

Handling Errors

Catch upstream exceptions with the catch operator.

numbers()
    .map { riskyTransform(it) }
    .catch { e -> emit(-1) }
    .collect { println(it) }

Reacting to Completion

Use onCompletion to run code when the flow finishes or is cancelled.

numbers()
    .onCompletion { println("done") }
    .collect { println(it) }

Changing the Producer Thread

Use flowOn to run the upstream on a specific dispatcher without affecting the collector.

fun load(): Flow<Data> = flow {
    emit(readFromDisk())
}.flowOn(Dispatchers.IO)

Terminal Operators

Some operators end the flow and return a value, like toList, first, or reduce. They are suspend functions.

val all: List<Int> = numbers().toList()

Where Flows Shine in Android

Flows model anything that emits over time: database changes (Room), location updates, search-as-you-type, and combining multiple data sources.

Quick Check

Test your understanding of Flow basics.

Recap

You learned:

  • flow { emit(...) } builds an async stream.
  • Flows are cold and only run when collected.
  • Transform with map/filter, handle errors with catch.
  • flowOn sets the producer dispatcher.
Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
36
Lessons
152

Frequently Asked Questions

Is the “Kotlin Flow Basics” lesson free?

Yes — any 3 lessons of the Android Academy learning path, including “Kotlin Flow Basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Android Academy course includes 4 lessons in total.

What will I learn in “Kotlin Flow Basics”?

Emit and collect streams of data. You practise Android 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 Android Academy?

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

How long does the “Kotlin Flow Basics” 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 Android Academy lesson?

Yes. Every Android 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. Coroutine Basics and Scopes
  2. viewModelScope and Dispatchers
  3. Kotlin Flow Basics
  4. StateFlow and SharedFlow
← Back to Android Academy