Home > database >  I am getting the error "Type mismatch: inferred type is List<Transactions.Past>? but List
I am getting the error "Type mismatch: inferred type is List<Transactions.Past>? but List

Time:04-22

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

Below is my 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)
        }
    }
}

Response From API is the following(data 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

}

I am unable to resolve this. Can someone help ?

CodePudding user response:

You can handle the error by providing an empty list:

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

Or if you want to handle this case as an error:

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