hello. I have a `Flow` which comes from the paging library. Until that flow can acutally be created ...
m
hello. I have a
Flow
which comes from the paging library. Until that flow can acutally be created (it needs some parameters from the user) i would like to create an empty flow that can be observed in compose. Something like
Copy code
val vacancies: Flow<PagingData<Vacancy>> = emptyFlow()
Now the question is: once i create that PagingData flow, how can i replace the
emptyFlow()
with the new paging data flow while still keeping the state observation from compose intact?
m
Assuming you don't want to use a MutableStateFlow (since you have no initial value), You could always create a state object with the flow, and then create a LaunchedEffect which will re-launch as the flow is replaced
Copy code
val vacancies = remember { mutableStateOf(emptyFlow<PagingData<Vacancy>>) }

...

LaunchedEffect(key1=vacancies.value) {
    vacancies.value.collect { item ->
    }
}
This should re-launch the coroutine in LaunchedEffect whenever you replace the value of vacancies flow. To be honest though i've never used a flow as key to a LaunchedEffect this way, but in theory it should work. Personally i'd rather be able to use MutableStateFlow with an initial value that tells me that no data is loaded yet, but that's a style choice.