Håkon Pettersen
09/01/2023, 11:49 AM/**
* Returns the presented item at the specified position, notifying Paging of the item access to
* trigger any loads necessary to fulfill prefetchDistance.
*
* @see peek
*/
operator fun get(index: Int): T? {
pagingDataDiffer[index] // this registers the value load
return itemSnapshotList[index]
}
Is the any any way to avoid using the non-null operator (!!) to access items, example:
@Composable
fun UserListContent(
users: LazyPagingItems<UserApiModel>,
) {
LazyVerticalGrid(columns = GridCells.Fixed(2)) {
items(count = users.itemCount, key = { users[it]?.id!! }) { index ->
UserListItem(user = users[index]!!)
}
}
}
Alex
09/01/2023, 1:17 PM!!
here looks unsafe on first look, why not just allow null as a key?Alex
09/01/2023, 1:20 PMnull
, that is part of the API, since you can access any index you’d like and there is no way for the list to ensure any item availability synchronously. null
usually (with paging) means the value is currently loading.myanmarking
09/01/2023, 1:58 PMIan Lake
09/01/2023, 2:02 PMitemKey
method exists (which does give you a non-null item): https://developer.android.com/jetpack/compose/libraries#pagingIan Lake
09/01/2023, 2:03 PMmyanmarking
09/01/2023, 2:03 PMHåkon Pettersen
09/01/2023, 2:25 PM