/personal-skills:kotlin-flow-state-event-modelingfrom personal-skills
Guides Kotlin StateFlow, SharedFlow, and Channel usage decisions: sentinel values, sharing modes, one-shot events, and synchronous reads.
Ships in personal-skills. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
Pick the primitive that matches replay, fan-out, and synchronous-read requirements. StateFlow, SharedFlow, Channel-backed flows, and cold Flow differ in buffering, who sees each emission, and whether .value exists. Wrong choices drop events, leak sharing coroutines, or force fake domain sentinels into state.
You're writing or reviewing Kotlin code involving:
MutableStateFlow<T>(SomeSentinel) — NoUser, Empty, Loading, etc. — because the real value is async.stateIn(...) called inside a function rather than assigned to a propertySharingStarted.WhileSubscribed(...) on a flow whose .value is read synchronously and must stay freshMutableSharedFlow for navigation events, snackbars, or other one-shot emissions where loss would be a bug.map { } on a StateFlow when consumers still need synchronous .valueMutableStateFlow.value = _state.value.copy(...) or update code that builds expensive objects inside update { ... }SharedFlow defaults have no replay buffer. If nothing is collecting at the exact instant of emission, the event is gone. For a single UI consumer handling exactly-once events such as navigation or snackbars, a buffered Channel exposed as a Flow often matches the semantics better:
// ❌ BAD
private val _navEvents = MutableSharedFlow<NavigationEvent>()
val navEvents: SharedFlow<NavigationEvent> = _navEvents.asSharedFlow()
// ✅ GOOD
private val _navEvents = Channel<NavigationEvent>(Channel.BUFFERED)
val navEvents: Flow<NavigationEvent> = _navEvents.receiveAsFlow()
Channel.receiveAsFlow() is fan-out, not broadcast: with multiple collectors, each event is delivered to one collector. Channel.BUFFERED is bounded, so sends can suspend and trySend can fail. If multiple observers must all see the same event, use explicit state, durable storage, or a deliberately configured SharedFlow instead.
StateFlow forces an initial value. When the real value is async, developers sometimes invent fake domain values — NoUser, EmptyUser, placeholder IDs — and every consumer is forced to treat that sentinel as real data.
// ❌ BAD — sentinel leaks into the type
class UserSession(private val db: Db) {
private val _user = MutableStateFlow<User>(NoUser)
val user: StateFlow<User> = _user.asStateFlow()
init { scope.launch { _user.value = db.load() } }
}
One fix is phasing: don't expose the StateFlow until the real value exists.
// ✅ GOOD — bootstrap suspends; observers only see real users
class UserSession(private val db: Db) {
private var _user: MutableStateFlow<User>? = null
val user: StateFlow<User>
get() = checkNotNull(_user) { "Call login() first" }
suspend fun login() {
_user = MutableStateFlow(db.load())
}
}
If absence, loading, or error is a real state, model it explicitly (User?, sealed interface UserUiState, Result, etc.). The bug is a fake domain value masquerading as real data, not every initial value.
update { ... }Prefer MutableStateFlow.update { current -> ... } over reading .value and writing it back. update applies the transform atomically against the latest state, which avoids lost updates when multiple coroutines mutate the same state.
// BAD — read/modify/write can lose concurrent updates.
_state.value = _state.value.copy(
selectedId = id,
details = details,
)
// GOOD — transform starts from the latest state.
_state.update { current ->
current.copy(
selectedId = id,
details = details,
)
}
Keep object creation outside the update block unless it needs the current state. The update lambda can be retried, so expensive work or side effects inside it may run more than once:
// GOOD — details does not depend on current state, so build it once.
val details = Details.from(response)
_state.update { current ->
current.copy(details = details)
}
// GOOD — derived value depends on current state, so compute it inside.
_state.update { current ->
val nextItems = current.items.replaceById(updatedItem)
current.copy(items = nextItems)
}
The block should be a pure, fast state transformation: no network calls, database writes, logging side effects, random IDs, or time reads unless those values were captured before the block.
stateIn() inside a function// ❌ BAD — new sharing coroutine every call
fun getPreferences(): StateFlow<Prefs> =
repo.prefsFlow.stateIn(scope, SharingStarted.Eagerly, Prefs.Default)
Every call to getPreferences() launches a fresh coroutine on scope that never completes. Performance dies fast under repeated reads.
// ✅ GOOD — one shared instance, computed once
val preferences: StateFlow<Prefs> =
repo.prefsFlow.stateIn(viewModelScope, SharingStarted.Eagerly, Prefs.Default)
WhileSubscribed with synchronous .valueSharingStarted.WhileSubscribed(timeout) disconnects the upstream when there are no active collectors. While disconnected, .value returns the last cached value, which may be stale or still the initial value.
Rule: if .value must be fresh or initialized without an active collector, use SharingStarted.Eagerly or explicit initialization. WhileSubscribed is fine when stale/cached values are acceptable and consumers primarily collect asynchronously.
.map on StateFlow loses .value// ❌ BAD — `name.value` won't compile; it's now a plain Flow
val name: Flow<String> = userState.map { it.name }
If you need synchronous .value, terminate the chain with .stateIn(...):
// ✅ GOOD
val name: StateFlow<String> = userState
.map { it.name }
.stateIn(viewModelScope, SharingStarted.Eagerly, userState.value.name)
Community “derived state flow” utilities run the transform on every .value read — only acceptable for fast, idempotent transforms. Default to .stateIn(...).
| Need | Primitive |
|---|---|
| State that always has a value, read by both async collectors and synchronous code | StateFlow, often with SharingStarted.Eagerly when .value matters |
Hot stream, multiple subscribers, no requirement for synchronous .value | SharedFlow |
| Discrete events for one consumer, exactly-once handoff | Consider Channel(BUFFERED).receiveAsFlow() |
| Cold stream, one consumer per collection | Plain Flow |
If you're tempted to reach for SharedFlow, ask: would dropping an emission be a bug, and how many consumers must see it? If one consumer must handle it exactly once, a Channel may fit. If every observer must see it, model durable state or configure a broadcast stream deliberately.
| Symptom | Problem | Fix |
|---|---|---|
MutableStateFlow<X>(FakeDomainValue) | Invalid placeholder default | Model absence explicitly or use phase initialization |
MutableSharedFlow<Event> for single-consumer nav/snackbar | Lossy default event stream | Consider Channel(BUFFERED).receiveAsFlow() |
fun foo() = flow.stateIn(...) | Per-call sharing coroutine | Make it a val / shared instance |
WhileSubscribed + .value must be fresh/initialized | Stale or initial data | SharingStarted.Eagerly or explicit initialization |
stateFlow.map { ... } consumed as state | Lost .value | Terminate with .stateIn(...) |
_state.value = _state.value.copy(...) | Non-atomic read/modify/write | _state.update { it.copy(...) } |
Expensive object creation inside update { ... } that doesn't use current state | Work can repeat if update retries | Build before update; keep only current-state transforms inside |
| Thought | Reality |
|---|---|
"We need SharedFlow because there are multiple subscribers" | Multiple subscribers change the semantics. Channel.receiveAsFlow() is not broadcast; choose the event model deliberately. |
"We'll use WhileSubscribed to save resources" | Only if stale/initial .value reads are acceptable. Verify before applying. |
| "I'll use a sentinel until real data loads" | Consumers treat it as real domain; prefer explicit UI/state modeling or phasing. |
"I'll construct the new object inside update because it's convenient" | The lambda may retry. Construct outside unless it depends on the current state. |
kotlin-coroutines-structured-concurrency — scope ownership, init launches, fire-and-forget boundaries, cancellation, runBlockingcompose-side-effects — collecting event flows and wiring side effects in Composecompose-state-holder-ui-split — where state holders expose flows to UIWhat SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
57 skills in this plugin
/android-data-layerUse when implementing the data layer in Android — Repository pattern, Room local database, offline-first synchronization, and coordinating local and remote sources./android-debuggingUse when debugging Android or KMP issues — supplements superpowers:systematic-debugging with Android-specific techniques covering Logcat, ADB, ANR traces, R8 stack trace decoding, memory leaks, Gradle build failures, and Compose recomposition bugs.
/android-devUse this skill as the baseline for ALL Android and Kotlin Multiplatform (KMP) work — whenever the user mentions Android, Kotlin (in an Android context), KMP, CMP, commonMain, androidMain, iosMain, AndroidManifest, Gradle, build.gradle, Hilt, Dagger, Room, Retrofit, Ktor, ViewModel, LiveData, StateFlow, SharedFlow, Compose, Activity, Fragment, Intent, ADB, Logcat, MVVM, MVI, repository pattern, or any Android SDK / Jetpack / AndroidX API. Always load this skill alongside more specific skills (android-skills:compose, android-skills:kotlin-flows, android-skills:kmp-ktor, android-skills:android-retrofit, etc.) — it provides the architectural baseline, existing-pattern audit, and project-adaptability rules those skills defer to. Casual mentions like "fix this bug in my Android app," "refactor this ViewModel," "my KMP project," or any work inside an Android project directory should trigger this skill./android-gradle-logicUse when setting up or refactoring Android Gradle build logic — convention plugins, composite builds, version catalogs, and shared build configuration across modules./android-retrofitUse when setting up or working with Retrofit in Android — service interface definitions, coroutines integration, OkHttp configuration, Hilt module setup, and error handling in the repository layer./android-source-searchUse when needing to fetch Android source code — AOSP platform internals (@hide APIs, framework classes, system services) or AndroidX/Jetpack library source and samples. Also use when public docs are insufficient to complete a task and implementation details must be read directly from source.2 plugins ship this exact skill · first indexed Jun 4, 2026
npx claudepluginhub w0lzard/wolzard-s-marketplace --plugin personal-skillsOther plugins' skills for the same job