Jerry Johns
05/10/2023, 5:37 AM@Composable
fun PanelContent(
viewModel: ArticlesViewModel,
magazine: ArticlesViewModel.MagazineState,
onShowSnackbar: (String) -> Unit,
) {
val articlesState by
remember(viewModel, articles) { viewModel.getArticlesState(magazine) }
.collectAsStateWithLifecycle()
articlesState?: return
LazyVerticalGrid(
columns = GridCells.Adaptive(minSize = 128.dp),
Modifier.absolutePadding(top = 8.dp, left = 16.dp, right = 16.dp, bottom = 8.dp).fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp),
horizontalArrangement = Arrangement.spacedBy(8.dp),
) {
val articles = articlesState!!.map { it.articleInfo }.toSet()
...
getArticlesState
returns back a StateFlow<Collection<ArticlesState>?>
. The null indicates the absence of any real value from our backend repository.
I'm observing that sometimes, I get a `NullPointerException`at articlesState!!
when it re-composes. How is that possible? Wouldn't the null check earlier have tripped?Sean Proctor
05/10/2023, 5:48 AMSean Proctor
05/10/2023, 5:51 AMarticleState
and check that value is non-null. Then the compiler could give you a guarantee and you wouldn't need the unsafe reference.Albert Chang
05/10/2023, 6:55 AMarticlesState?.let { LazyVerticalGrid() }
.Albert Chang
05/10/2023, 6:57 AMJerry Johns
05/11/2023, 8:00 AM