I have a flow mapping like this: ```var selectedGr...
# compose
j
I have a flow mapping like this:
Copy code
var selectedGroup: MutableStateFlow<Group?> = MutableStateFlow(null)
...
private val _userInfosFlow = APIServer.getAllUserInfosAsFlow()
val userInfos: StateFlow<List<UserInfo>> =
    _userInfosFlow.map { userInfos ->
        selectedGroup.value?.let { group ->
            userInfos.filter { userInfo ->
                userInfo.groupId == group.getId()
            }
        } ?: emptyList()
    }
Where
_userInfosFlow
just gets a list of all UserInfo objects from database, and
userInfos
is a StateFlow that filters that list to only contain UserInfo's belonging to the current
selectedGroup
. Since I use
.map
, I believe the
userInfos
flow will emit only when
_userInfosFlow
emits. However, I also want
userInfos
to emit when
selectedGroup.value
changes, since that means that a new group is selected, and
userInfos
should logically update to now contain UserInfo's from the newly selected group. What is the best way to trigger this emission? Or is there just a better way to do this altogether
a
This is a #coroutines question. You can use `combine`:
Copy code
combine(_userInfosFlow, selectedGroup) { userInfos, group ->
    ...
}