如何将流<PagingData<Data>>声明为变量

问题描述 投票:0回答:1

我正在尝试使用 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()
android android-jetpack-compose android-paging-3 data-paging
1个回答
0
投票

就这么简单

val breakingNews = Pager(
    config = PagingConfig(
        pageSize = 10,
    ),
    pagingSourceFactory = {
        DocPagingSource(arkeliyaApiService)
    }
).flow.cachedIn(viewModelScope)

val documentList = dashboardViewModel.breakingNews.collectAsLazyPagingItems()
© www.soinside.com 2019 - 2024. All rights reserved.