我正在尝试使用 peging 3 库获取 API 数据。我正在关注本教程。但是,该 API 会被多次调用。我认为这是因为 compose 中的这段代码:
val documentList = dashboardViewModel.getBreakingNews().collectAsLazyPagingItems()
这是视图模型的代码
fun getBreakingNews(
): Flow<PagingData<Data>> = getNews().cachedIn(viewModelScope)
fun getNews() = Pager(
config = PagingConfig(
pageSize = 10,
),
pagingSourceFactory = {
DocPagingSource(arkeliyaApiService)
}
).flow
我想声明
Flow<PagingData<Data>>
就像我对其他 API 所做的那样
例如
private val _listofReqDoc = MutableStateFlow<List<Data>>(listOf())
val listofReqDoc = _listofReqDoc.asStateFlow()
就这么简单
val breakingNews = Pager(
config = PagingConfig(
pageSize = 10,
),
pagingSourceFactory = {
DocPagingSource(arkeliyaApiService)
}
).flow.cachedIn(viewModelScope)
val documentList = dashboardViewModel.breakingNews.collectAsLazyPagingItems()