I have an empty list as initial value to show skeleton loadings on recyclerview but the problem is the initial value doesn't get emmited when collected inside fragment and only receive the second value emitted from ViewModel after loading data.
ViewModel:
private val _orderHistoryList = MutableStateFlow(
PagingData.from(Array(6) { OrderDetail(id = - 1L * it) }.toMutableList())
)
val orderHistoryList: StateFlow<PagingData<OrderDetail>> = _orderHistoryList
init {
viewModelScope.launch {
getOrderHistory.execute()
.cachedIn(viewModelScope)
.collect {
_orderHistoryList.value = it
}
}
}
Fragment:
lifecycleScope.launch {
repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.orderHistoryList.collect {
adapter.submit(it)
}
}
}