If you read the items of a flow with first(), docu...
# compose
p
If you read the items of a flow with first(), documentation says that then it's cancelled and doesn't receive any changes. But I remember something similar to this in a codelab, they were using .first() to retrieve UiState values... and UiState values needs to be updated with every change. Will it work to use .first() on a Flow to set the values of an UiState in a ViewModel?
Copy code
viewModelScope.launch {
    val airports = flightRepository.getAirports()
    val favorites = flightRepository.getFavorites()
    val searchText = userPreferencesRepository.searchText

    uiState = uiState.copy(
        airports = airports.filterNotNull().first(),
        favorites = favorites.filterNotNull().first(),
        searchText = searchText.first()
    )
}
m
This seems to be designed to just show the current state and not any updates.
z
Looks like you’d want to do something like
Copy code
combine(airports, favorites, searchText) { a, f, s ->
  uiState = uiState.copy(a, f, s)
}.collect()
p
Hi zach can you expand that pseudocode sample into a working function?
thank you
maybe is there any minor change that can be donde to that launch coroutine that makes that those variables are being updated every time a change has been produced to the flow?
z
Look up “kotlin flow combine”, what you’re trying to do is pretty basic so the sample code applies almost directly.
p
Hi zach, maybe we are talking about different topics, my issue is not with combine, which "Returns a Flow whose values are generated with transform function by combining the most recently emitted values by each flow.". My issue is with simply receiving constantly the new values, not combining them.
z
Something more like this?
Copy code
launch {
  airports.collect {
    uiState = uiState.copy(airports = it)
  }
}
launch {
  favorites.collect {
    uiState = uiState.copy(favorites = it)
  }
}