kotlin-coroutines-flows

Implements structured concurrency, Flow operators, and coroutine testing patterns for Kotlin Android and KMP projects.

Updated Mar 25, 2026
One-click install
npx skills add https://github.com/Femad-6/my-skills --skill kotlin-coroutines-flows-femad-6
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-coroutines-flows
Source: https://github.com/Femad-6/my-skills/tree/main/.github/skills/kotlin-coroutines-flows
Command: npx skills add https://github.com/Femad-6/my-skills --skill kotlin-coroutines-flows-femad-6

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Writing asynchronous Kotlin code without structured concurrency leads to leaked coroutines, unhandled cancellation, and untestable reactive streams. This Skill provides proven patterns for coroutines, Flow, StateFlow, and SharedFlow in Android and Kotlin Multiplatform projects. ## Core Features & Use Cases - Structured Concurrency: Apply viewModelScope, coroutineScope, supervisorScope, and parallel async decomposition instead of GlobalScope. - Flow Patterns: Build StateFlow UI state with stateIn and WhileSubscribed, combine multiple flows, debounce search input, retry with exponential backoff, and emit one-time events via SharedFlow. - Testing Support: Test StateFlow with Turbine, control time with runTest and TestDispatcher, and fake Flow-based repositories. - Use Case: When building a dashboard screen that loads items, stats, and profile in parallel and exposes combined UI state, use this Skill to structure the coroutines and expose a single StateFlow safely. ## Quick Start Ask the assistant to refactor a ViewModel to load data in parallel with coroutineScope and expose the result as a StateFlow using stateIn with WhileSubscribed.

Frequently Asked Questions about kotlin-coroutines-flows

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I run coroutines in parallel in Kotlin?

Use coroutineScope with multiple async blocks to run suspending work concurrently, then call await on each Deferred. This keeps the work structured so a failure cancels siblings and the parent waits for all children.

How to convert a Flow to StateFlow in Android ViewModel?

Call stateIn on the Flow with viewModelScope, SharingStarted.WhileSubscribed(5000), and an initial value. WhileSubscribed keeps the upstream active for five seconds after the last subscriber leaves, surviving configuration changes.

StateFlow vs SharedFlow: when should I use each?

Use StateFlow for UI state that always has a current value and should be replayed to new collectors. Use SharedFlow for one-time events like snackbars or navigation that should not be re-delivered after rotation.

Does Dispatchers.IO work in Kotlin Multiplatform?

Dispatchers.IO is only available on JVM and Android. In KMP common code, use Dispatchers.Default or Dispatchers.Main, which exist on all platforms, or inject the IO dispatcher through dependency injection per platform.

How do I test StateFlow emissions in Kotlin?

Use runTest with the Turbine library: call state.test and assert each awaitItem in sequence. Combine with advanceUntilIdle and a TestDispatcher to control virtual time, and fake repositories backed by MutableStateFlow.

Why is using GlobalScope in Kotlin coroutines a problem?

GlobalScope launches coroutines with no parent, so they leak beyond the lifecycle that started them and cannot be cancelled structurally. Use viewModelScope, lifecycleScope, or a child coroutineScope instead.