19 Things to Know About Kotlin Flow — A Quick Note

Quick look at some useful things you should know about Kotlin Flow.
May 26 2022 · 2 min read


Do you have one minute? great!

It’s enough to have quick look at some useful things you should know about Kotlin Flow.

Flow is an asynchronous data stream that sequentially emits values and completes normally or with an exception.

We are what we repeatedly do. Excellence, then, is not an act, but a habit. Try out Justly and start building your habits today!

19 Things to know about Kotlin Flow

  1. Flows is basically cold stream, but flow can be converted into a hot stream by stateIn or shareIn operators.
  2. StateFlow and SharedFlow are the hot stream variants.
  3. Under the hood StateFlow is a shared flow. State flow never completes. State flow requires an initial value.
  4. All methods of state flow and shared flow are thread-safe and can be safely invoked from concurrent coroutines without external synchronization.
  5. The Flow interface does not carry information on whether a flow is a cold stream or hot stream.
  6. Intermediate operators (map, filter, zip, etc…) do not execute any code in the flow and are not suspending functions themselves. They only set up a chain of operations for future execution and quickly return.
  7. Terminal operators (single, toList, collects, etc…) are suspending functions. Terminal operators complete normally or exceptionally depending on the successful or failed execution of all the flow operations in the upstream.
  8. Flow implementation never catch or handle an exception that occurs in downstream, it only catches upstream exceptions in catch operator.
  9. StateFlow always filters out repetitions of the same value.
  10. Flow is not life-cycle aware. Use StateFlow with repeatOnLifecycle or flowWithLifecycle to make it lifecycle aware. Both APIs are using for safely collecting flows in UI layer.
  11. Use repeatOnLifecycle to collect multiple flow. You can launch multiple flow with repeatOnLifecycle, whereas use flowWithLifecycle to collect single Flow.
  12. repeatOnLifecycle prevents you from wasting resources, as it restarts and stops the flow collection on the targeted state of lifecycle change.
  13. Unlike the normal flow, SharedFlow is something like a Radio station, which has multiple listeners — a single flow that will get shared by multiple consumers. Radio stations do continue to broadcast even if there’s no listener, the same things apply to SharedFlow
  14. With SharedFlow an event will be lost if the consumer is not ready to receive events.
  15. Do not use shareIn or stateIn in function which returns the Flow, this will create a new Flow on each invocation.
  16. To keep your upstream flow active after the last collector stop collecting, use sharing started strategy — WhileSubscribed in shareIn & stateIn
  17. Sharing started strategy — Eagerly & Lazily keep your flow active till the coroutine scope is active.
  18. onCompletion callback of Flow does not handle exception itself, use catch or wrap in to try/catch to handle exceptions.
  19. flow{} is not allowed to emit from different context(ex. by using withContext()), use flowOn to change the upstream context.

radhika-s image
Radhika saliya
Android developer | Sharing knowledge of Jetpack Compose & android development

radhika-s image
Radhika saliya
Android developer | Sharing knowledge of Jetpack Compose & android development

Talk to an expert
get intouch
Our team is happy to answer your questions. Fill out the form and we’ll get back to you as soon as possible
Subscribe Here!
Follow us on
2024 Canopas Software LLP. All rights reserved.