Flow for Kotlin: Complete Guide with Examples

Online Python Trainer for Beginners

Learn Python easily without overwhelming theory. Solve practical tasks with automatic checking, get hints in Russian, and write code directly in your browser — no installation required.

Start Course

Introduction to Flow: What is it and why is it needed?

\n\n

Flow is a library for working with reactive data streams in the Kotlin programming language. It is part of the Kotlin Coroutines ecosystem and provides a powerful, type-safe, and asynchronous way to process sequences of data that are generated over time.

\n\n

Unlike regular collections (List, Set), which store all elements in memory at once, Flow is a cold stream. This means that data starts being generated only when a subscriber (collector) appears. Flow is ideal for:

\n
    \n
  • Asynchronous data processing: Fetching data from the network, database, or file system without blocking the main thread.
  • \n
  • Event handling: Clicks, mouse movements, UI updates.
  • \n
  • Reactive programming: Creating chains of data transformations (map, filter, flatMap) with automatic error handling and cancellation.
  • \n
  • Integration with coroutines: Flow is fully compatible with Kotlin Coroutines, making it easy to combine with other asynchronous operations.
  • \n
\n\n

Why use Flow instead of older approaches (e.g., RxJava or callbacks)?

\n
    \n
  • Simplicity and conciseness: The Flow API is minimalistic and intuitive for Kotlin developers.
  • \n
  • Safety: Type safety at the compiler level.
  • \n
  • Integration with coroutines: Leverages all the benefits of structured concurrency.
  • \n
  • Performance: Lightweight and efficient, does not create unnecessary objects.
  • \n
\n\n

Installing Flow

\n\n

Flow is part of the kotlinx.coroutines library. To use it, you need to add a dependency to your build.gradle.kts (for Gradle) or pom.xml (for Maven) file.

\n\n

Gradle (Kotlin DSL)

\n
dependencies {\n    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")\n    // For using Flow in Android (with Lifecycle support)\n    implementation("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")\n}
\n\n

Gradle (Groovy)

\n
dependencies {\n    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1'\n    implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1'\n}
\n\n

Maven

\n
\n    org.jetbrains.kotlinx\n    kotlinx-coroutines-core\n    1.8.1\n
\n\n

Important: Make sure the Kotlin version and the kotlinx-coroutines version are compatible. The latest version can be found on GitHub.

\n\n

Quick Start: Your First Flow

\n\n

Let's create a simple Flow that emits three numbers with a delay and subscribe to it.

\n\n
import kotlinx.coroutines.*\nimport kotlinx.coroutines.flow.*\n\nfun simpleFlow(): Flow<Int> = flow {\n    println("Flow started")\n    for (i in 1..3) {\n        delay(100) // Simulating asynchronous work\n        emit(i)    // Emitting the value\n    }\n}\n\nfun main() = runBlocking {\n    println("Main thread: ${Thread.currentThread().name}")\n\n    // Subscribing to the Flow (collecting data)\n    simpleFlow().collect { value ->\n        println("Received: $value")\n    }\n\n    println("Flow completed")\n}
\n\n

Output:

\n
Main thread: main\nFlow started\nReceived: 1\nReceived: 2\nReceived: 3\nFlow completed
\n\n

Explan

Recommendations