Kotlin Flow Conflation Can Silently Drop UI Events, Causing Lost Receipts

A payments app intermittently failed to show receipt screens even though the backend and ViewModel logs looked normal. The root cause is conflation in Kotlin Flow, where a producer outpacing its collector causes intermediate values to be discarded and only the latest one delivered. StateFlow does this by design, which suits continuously changing state like progress or search queries but is harmful for one-off events such as navigation or dialogs. Modeling events as nullable state fields can cause messages to be overwritten or suppressed entirely, since StateFlow also skips emissions equal to the current value. The article recommends choosing a primitive that matches whether a value represents state or an event, such as SharedFlow for events.
This is an AI-generated summary. ShortSingh links to the original source for the complete article.

Discussion (0)
Log in to join the discussion and vote.
Log in