kotlin-coroutines-flows

Guide asynchronous Kotlin code with coroutines and Flow patterns.

1|Updated Apr 7, 2026
One-click install
npx skills add https://github.com/riftzen-bit/gemini-setup --skill kotlin-coroutines-flows-riftzen-bit
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-coroutines-flows
Source: https://github.com/riftzen-bit/gemini-setup/tree/main/skills/kotlin-coroutines-flows
Command: npx skills add https://github.com/riftzen-bit/gemini-setup --skill kotlin-coroutines-flows-riftzen-bit

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

It reduces bugs and lifecycle-related leaks caused by incorrect coroutine usage and Flow handling by providing clear patterns for structured concurrency, cancellation, dispatcher selection, and reactive UI state management.

Core Features & Use Cases

  • Structured Concurrency: guidance for scoping coroutines to ViewModel, composables, and application lifecycles to avoid leaks.
  • Flow Patterns: techniques for StateFlow, SharedFlow, cold flows, combining multiple flows, and operator usage such as debounce, distinctUntilChanged, flatMapLatest, and retry strategies.
  • Dispatchers & Cancellation: recommendations for choosing Dispatchers across Android and KMP, cooperative cancellation, and cleanup with try/finally.
  • Testing Strategies: examples for using TestDispatcher, runTest, and Turbine to assert StateFlow and Flow behavior.
  • Use Case: refactor a dashboard ViewModel to load items and stats in parallel with proper error isolation, expose a StateFlow UI model, and emit one-time navigation/snackbar events with SharedFlow.

Quick Start

Use the kotlin-coroutines-flows skill to review and refactor a ViewModel to use coroutineScope with async for parallel loading, StateFlow for UI state, and TestDispatcher-based tests.

Frequently Asked Questions about kotlin-coroutines-flows

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

FAQPage Schema
How do I manage UI state with StateFlow and SharedFlow in Kotlin coroutines?

StateFlow manages UI state by exposing a single updated value to collectors, while SharedFlow handles one-time events like navigation or snackbars. Both integrate with Kotlin coroutines to coordinate reactive streams and ensure structured concurrency.

How do I test Kotlin Flow and StateFlow using TestDispatcher and Turbine?

Test Kotlin Flow and StateFlow by using runTest with TestDispatcher to control virtual time and Turbine to assert emissions. This strategy validates coroutine testing scenarios, cooperative cancellation, and retry/backoff behaviors deterministically.

Can I use structured concurrency and coroutine patterns in Kotlin Multiplatform projects?

Yes, structured concurrency patterns apply to Kotlin Multiplatform projects. You can scope coroutines to application lifecycles, select appropriate dispatchers across platforms, and coordinate concurrency without leaks in shared KMP code.

What's the best way to handle parallel loading and error isolation in an Android ViewModel?

Refactor the Android ViewModel to use coroutineScope with async for parallel loading. This structured concurrency approach loads items and stats concurrently while isolating errors properly, then aggregates results into a StateFlow UI model.

Why do my Kotlin coroutine flows leak memory and miss cooperative cancellation?

Coroutines leak memory when not scoped to lifecycles, and miss cooperative cancellation without proper cleanup. Apply structured concurrency by scoping to ViewModel or composables, and use try/finally blocks to ensure resources are released.