kotlin-flow-state-event-modeling

Guide Kotlin Flow primitive selection for state and event modeling.

908|42|Updated May 12, 2026
One-click install
npx skills add https://github.com/chrisbanes/skills --skill kotlin-flow-state-event-modeling-chrisbanes
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-flow-state-event-modeling
Source: https://github.com/chrisbanes/skills/tree/main/skills/kotlin-flow-state-event-modeling
Command: npx skills add https://github.com/chrisbanes/skills --skill kotlin-flow-state-event-modeling-chrisbanes

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This section helps developers pick the correct Kotlin Flow primitives to model state and events in their apps, avoiding misuses that can lead to lost events, stale values, or complex state transitions.

Core Features & Use Cases

  • Guidance on when to use StateFlow, SharedFlow, Channel, and stateIn to model synchronous read requirements, replay semantics, and one-shot events.
  • Best practices for avoiding sentinel defaults, performing atomic updates with update, and choosing appropriate SharingStarted strategies for robust UI updates.
  • Real-world scenarios across Kotlin and Android development to drive correct flow composition and event handling.

Quick Start

Apply the Kotlin Flow state/event modeling guidance to optimize state and event handling in your Flow-based code.

Frequently Asked Questions about kotlin-flow-state-event-modeling

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

FAQPage Schema
When should I use StateFlow vs SharedFlow for Kotlin state and event modeling?

Use StateFlow for synchronous state reads and SharedFlow for event modeling with replay semantics. StateFlow requires an initial value and emits distinct updates, while SharedFlow supports replaying events to multiple subscribers.

How do I handle one-shot events in Kotlin Flow without losing data?

To handle one-shot events safely, use Channel for synchronous event delivery or SharedFlow with appropriate replay semantics. Channels ensure events are consumed once, preventing lost events during complex state transitions.

What is the best way to update StateFlow atomically in Kotlin?

The best way to update StateFlow atomically is using the update function. It applies atomic updates to MutableStateFlow, preventing race conditions during complex state transitions and ensuring thread-safe UI updates.

How does stateIn work for sharing Kotlin Flow strategies?

The stateIn function converts a cold Flow into a hot StateFlow by applying sharing strategies. It requires a SharingStarted strategy to control when sharing begins and ends, ensuring robust UI updates across Android and Kotlin projects.

Why should I avoid sentinel default values in Kotlin StateFlow?

Avoiding sentinel default values in StateFlow prevents stale values and complex state transitions. Using meaningful initial states instead of sentinels ensures error-resistant handling and correct flow composition for UI updates.

Does Kotlin Channel work better than SharedFlow for event synchronization?

Channel offers synchronized event delivery for one-shot events, ensuring each event is consumed by exactly one collector. SharedFlow broadcasts events to all subscribers, making it suitable when multiple consumers need the same event.