0

An expense item list is retrieved from the Room database, as shown below. And I rearranged them by date using '.flatMapConcat {}' in the Repository.

override fun getMonthlyStream(): Flow<List<AssetEntity>> {
    return dbDao.getMonthlyData("04")
        .flatMapConcat { entities ->
            flow {
        ...

And the ViewModel provides the list to the UI as a 'StateFlow' type. At this time, when the first View is drawn by the 'initial' parameter of the '.stateIn()' operator, it is initially passed as an empty list.

val calendarUiState: StateFlow<List<AssetEntity>> =
    repository.getMonthlyStream()
        .stateIn(
            scope = viewModelScope,
            started = SharingStarted.WhileSubscribed(5_000L),
            initialValue = emptyList()
        )

Because of this, when ViewModel data is loaded from View, an Empty list is first entered, and the list size is always 0 even after recomposition has occurred.

viewModel: CalendarViewModel = viewModel(factory = AppViewModelProvider.Factory)
) {
    val calendarUiState by viewModel.calendarUiState.collectAsState()
    var sizel by remember { mutableStateOf(calendarInput.size) } // Always 0

How can I solve the above problem? Also, should I use 'stateFlow' for what I'm trying to do?

baek9
  • 1

0 Answers0