Jerry Johns
05/09/2023, 1:34 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?Pablichjenkov
05/10/2023, 5:44 AMStylianos Gakis
05/10/2023, 7:30 AMremember
it, on each recomposition they’ll grab a new instance of it, and start collecting that new instance, each time.
If anything, the “odd” part there is that the key on remember is articles
but the parameter passed into the getArticlesState
is magazine
but that’s not a key in the remember.Pablichjenkov
05/10/2023, 11:37 AM