我在 PagingSource class 中收到错误 "Type mismatch: inferred type is List<Transactions.Past>? but List<TypeVariable(Value)> was expected"

I am getting the error "Type mismatch: inferred type is List<Transactions.Past>? but List<TypeVariable(Value)> was expected" in PagingSource class

我在 PagingSource class 中尝试实现 Paging 3 时收到错误“类型不匹配:推断类型为 List? 但 List 是预期的”科特林

下面是我的 PagingSource class

class ptPagingSource(
    private val repository: StackRepository
) : PagingSource<Int,Transactions.Past>() {

    /**
     * The refresh key is used for subsequent calls to PagingSource.Load after the initial load.
     */
    override fun getRefreshKey(state: PagingState<Int,Transactions.Past>): Int? {
        // We need to get the previous key (or next key if previous is null) of the page
        // that was closest to the most recently accessed index.
        // Anchor position is the most recently accessed index.
        return state.anchorPosition?.let { anchorPosition ->
            state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1)
                ?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1)
        }

    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int,Transactions.Past> {
        val pageIndex=params.key?: PAST_TRANSACTIONS_STARTING_PAGE_INDEX

        return try {
            val response = repository.getPastTransactions(
                accountNumber = "02-1280-0000711-010",
                pageSize = PAGE_SIZE,
                pageNumber = pageIndex

            )
            val pastTransactionsList = response.value?.body
            

            val nextKey =
                if (pastTransactionsList?.isEmpty() == true) {
                    null
                } else {
                    // By default, initial load size = 3 * NETWORK PAGE SIZE
                    // ensure we're not requesting duplicating items at the 2nd request
                    pageIndex + (params.loadSize / PAGE_SIZE)
                }
            LoadResult.Page(
                data = pastTransactionsList,    //Getting error in this line
                prevKey = if (pageIndex == PAST_TRANSACTIONS_STARTING_PAGE_INDEX) null else pageIndex,
                nextKey = nextKey
            )
        } catch (exception: IOException) {
            return LoadResult.Error(exception)
        } catch (exception: HttpException) {
            return LoadResult.Error(exception)
        }
    }
}

来自 API 的响应如下(数据 class)

sealed class Transactions{

@Parcelize
    data class Past(
        val accountNumber: String,
        val balance: Double,
        val createDate: String,
        val description: String,
        val effectiveDate: String,
        val referenceNumber: String,
        val transactionAmount: Double
    ) : Parcelable

}

我无法解决这个问题。有人可以帮忙吗?

您可以通过提供一个空列表来处理错误:

val pastTransactionsList = response.value?.body ?: emptyList<Transactions.Past>()
...

或者如果您想将这种情况作为错误处理:

val pastTransactionsList = response.value?.body
...
if (pastTransactionsList != null) {
    LoadResult.Page(...)
} else {
    LoadResult.Error(...)
}