kotlin-flow-state-event-modeling

Reviews and guides Kotlin Flow state and event modeling with StateFlow, SharedFlow, and Channel.

Updated Jul 1, 2026
One-click install
npx skills add https://github.com/w0lzard/Wolzard-s-Marketplace --skill kotlin-flow-state-event-modeling-w0lzard
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: kotlin-flow-state-event-modeling
Source: https://github.com/w0lzard/Wolzard-s-Marketplace/tree/main/plugins/personal-skills/skills/kotlin-flow-state-event-modeling
Command: npx skills add https://github.com/w0lzard/Wolzard-s-Marketplace --skill kotlin-flow-state-event-modeling-w0lzard

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Kotlin developers frequently misuse Flow primitives: SharedFlow events get silently dropped, StateFlow gets polluted with fake sentinel values like NoUser, stateIn() leaks sharing coroutines when called inside functions, and WhileSubscribed serves stale .value reads. This Skill provides concrete decision rules and code patterns to pick the right primitive and avoid these bugs. ## Core Features & Use Cases - Primitive selection guidance: Decision table mapping requirements (replay, fan-out, synchronous .value reads) to StateFlow, SharedFlow, Channel.receiveAsFlow(), or cold Flow. - Common bug detection: Identifies anti-patterns such as sentinel initial values, stateIn() inside functions, non-atomic read/modify/write on MutableStateFlow, and expensive work inside update { } blocks. - Use Case: While reviewing a ViewModel, you notice MutableSharedFlow used for navigation events and _state.value = _state.value.copy(...) mutations. The Skill flags the lossy event stream and non-atomic update, then shows the Channel(BUFFERED).receiveAsFlow() and update { } replacements. ## Quick Start Review this ViewModel's StateFlow and SharedFlow usage and tell me whether the event and state primitives are modeled correctly.

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

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

FAQPage Schema
How do I emit one-shot events like navigation from a Kotlin ViewModel?

For a single UI consumer handling exactly-once events, use Channel<NavigationEvent>(Channel.BUFFERED) exposed via receiveAsFlow(). Default MutableSharedFlow has no replay buffer, so events emitted with no active collector are silently lost.

StateFlow vs SharedFlow: which should I use in Kotlin?

Use StateFlow when state always has a value and consumers need synchronous .value reads. Use SharedFlow for hot streams with multiple subscribers and no .value requirement. For single-consumer exactly-once events, consider Channel(BUFFERED).receiveAsFlow().

Why does stateIn inside a function cause problems?

Calling stateIn() inside a function launches a new sharing coroutine on the scope with every call, and those coroutines never complete. Assign stateIn to a val property once so a single shared instance is reused.

Does SharingStarted.WhileSubscribed keep StateFlow .value fresh?

No. WhileSubscribed disconnects upstream when there are no collectors, so .value returns the last cached or initial value. If synchronous .value must be fresh or initialized, use SharingStarted.Eagerly or explicit initialization.

Why use MutableStateFlow.update instead of setting .value directly?

update { } applies the transform atomically against the latest state, avoiding lost updates from concurrent read/modify/write. Keep expensive object creation outside the lambda since it may be retried, unless it depends on current state.