Questions tagged [kotlin-flow]

In Kotlin coroutines, a Flow is a stream of asynchronously computed values. Use this tag when you're having problems building, transforming or consuming flows.

In Kotlin coroutines, a Flow is stream of asynchronously computed values.

Documentation: https://kotlinlang.org/docs/flow.html#flows

895 questions
16
votes
2 answers

Use of SharedFlow in Android kotlin

Hey I am learning flow in kotlin. I am learning about MutableStateFlow and MutableSharedFlow. I tried to learn MutableStateFlow in real world example. But I am unable to get the MutableSharedFlow example, which place it suits more. I tried some…
16
votes
2 answers

Jetpack Compose – LazyColumn not recomposing

My LazyColumn is not recomposing but the value is getting updated. If I scroll down the list and scroll back up I see correct values for the UI MainActivity class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState:…
16
votes
8 answers

Combine two state flows into new state flow

I have two state flows. Is it possible to combine them and get new state flow? Logically it should be possible because both state flows have initials values, but as I see combine function returns just Flow and not StateFlow.
Kiryl Tkach
  • 3,118
  • 5
  • 20
  • 36
16
votes
3 answers

Kotlin Flow: Why the function combine() can only take maximum 5 flows in parameters

I notice the combine() function can only take maximum 5 flows in parameters public fun combine( flow: Flow, flow2: Flow, flow3: Flow, flow4: Flow, flow5: Flow, transform: suspend…
Luxi Liu
  • 7,878
  • 2
  • 14
  • 13
15
votes
3 answers

Difference between launchWhenStarted and repeatOnLifecycle(STARTED) in collecting flows

As launchWhenStarted and repeatOnLifecycle(STARTED) provide completely different functionality (launchWhenStarted suspends the execution of the coroutine, and repeatOnLifecycle cancels and restarts a new coroutine), if the names of the new APIs…
Ahmad Albatsh
  • 314
  • 1
  • 2
  • 12
15
votes
1 answer

What are the differences between StateFlow and LiveData?

as I mentioned in the title, I'm curious about the general differences between the two. Can you help with this? I couldn't find the specific differences as there are complex examples on the internet. What are the differences in terms of…
Arda Kazancı
  • 8,341
  • 4
  • 28
  • 50
14
votes
3 answers

How to safely (lifecycle aware) .collectAsState() a StateFlow?

I'm trying to follow the official guidelines to migrate from LiveData to Flow/StateFlow with Compose, as per these articles: A safer way to collect flows from Android UIs Migrating from LiveData to Kotlin’s Flow I am trying to follow what is…
14
votes
2 answers

How to collect multiple state flow in Android

How to collect two state flow in activity? Because mine only the first flow that consumed. For example, inside viewmodel is like this: class ExampleViewModel: ViewModel(){ private val state =…
13
votes
2 answers

Android - How to read a value from a kotlin flow?

I have a flow of Episode from a room database. I can observe as livedata this flow with no problem. But I also would like to read the last value from this flow when the user clicks on a button. I tried with first() terminal flow operator but it…
u2gilles
  • 6,888
  • 7
  • 51
  • 75
13
votes
2 answers

Room with Flow returns null when empty

I've just started looking at Room, Coroutines, and Flow, and have come across something odd: what I'm expecting to be an empty flow actually has one null item in it. My setup is as follows, with generic T for my actual entities. interface TDao { …
straphe
  • 155
  • 1
  • 10
13
votes
3 answers

Kotlin Flow execute two API calls in parallel and collect each result as it arrives

I am trying to implement cache then network strategy for my API call using Kotlin Flows. Here is what I am trying right now flowOf( remoteDataSource.getDataFromCache() // suspending function returning Flow .catch { error -> Timber.e(error)…
12
votes
3 answers

Jetpack compose collectAsState() is not collecting a hot flow when the list is modified

When I use collectAsState(), the collect {} is triggered only when a new list is passed, not when it is modified and emitted. View Model @HiltViewModel class MyViewModel @Inject constructor() : ViewModel() { val items =…
12
votes
1 answer

MutableSharedFlow - difference between replay and extraBufferCapacity

MutableSharedFlow takes 3 parameters: replay, extraBufferCapacity and onBufferOverflow. What is the difference between replay and extraBufferCapacity? The documentation mentions the following: replay - the number of values replayed to new…
12
votes
4 answers

Android Flow vs StateFlow

I recently started using Flows in Android. I read that Flows are cold StateFlows are hot, then why should we prefer using StateFlows for Android over Flows? Won't it be better to use Flows as they will stop the producer when the app goes to the…
pvn
  • 2,016
  • 19
  • 33
12
votes
2 answers

Elegant way of handling error using Retrofit + Kotlin Flow

I have a favorite way of doing network request on Android (using Retrofit). It looks like this: // NetworkApi.kt interface NetworkApi { @GET("users") suspend fun getUsers(): List } And in my ViewModel: // MyViewModel.kt class…
seemuch
  • 233
  • 1
  • 2
  • 11
1
2
3
59 60