java.lang.illegalstateException:在滚动式的懒惰列中,应将layoutnode附加到所有者
我正在使用lazycolumn和第3页上的lazycolumn来加载联系人,当我滚动列表时,有时会引发以下例外,
java.lang.IllegalStateException: LayoutNode should be attached to an owner
at androidx.compose.ui.node.LayoutNodeKt.requireOwner(LayoutNode.kt:1407)
at androidx.compose.ui.node.ModifierLocalConsumerNode.notifyConsumerOfChanges(ModifierLocalConsumerNode.kt:42)
以下是LazyColumn的代码:
val contactsFlow= viewModel.fetchContacts(context)
val lazyContacts = contactsFlow.collectAsLazyPagingItems()
LazyColumn(
modifier = Modifier.padding(top = 8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)) {
items(lazyContacts) { contact ->
if(contact != null)
{
Contact(contact = contact, onContactClick = {
viewModel.addMemberFrom(it) { member ->
onContactClick(member)
}
})
}
}
}
这是ViewModel代码:
fun fetchContacts(context: Context): Flow<PagingData<Contact>> {
return Pager(
PagingConfig(10, enablePlaceholders = true, initialLoadSize = 20)
) {
ContactsPagingSource(contactManager, context)
}.flow.cachedIn(viewModelScope)
}
I am using LazyColumn with Paging 3 to load contacts, when I scroll the list its laggy and sometimes it throws the following exception
java.lang.IllegalStateException: LayoutNode should be attached to an owner
at androidx.compose.ui.node.LayoutNodeKt.requireOwner(LayoutNode.kt:1407)
at androidx.compose.ui.node.ModifierLocalConsumerNode.notifyConsumerOfChanges(ModifierLocalConsumerNode.kt:42)
Following is the code for LazyColumn:
val contactsFlow= viewModel.fetchContacts(context)
val lazyContacts = contactsFlow.collectAsLazyPagingItems()
LazyColumn(
modifier = Modifier.padding(top = 8.dp),
verticalArrangement = Arrangement.spacedBy(8.dp)) {
items(lazyContacts) { contact ->
if(contact != null)
{
Contact(contact = contact, onContactClick = {
viewModel.addMemberFrom(it) { member ->
onContactClick(member)
}
})
}
}
}
Here is the viewModel code:
fun fetchContacts(context: Context): Flow<PagingData<Contact>> {
return Pager(
PagingConfig(10, enablePlaceholders = true, initialLoadSize = 20)
) {
ContactsPagingSource(contactManager, context)
}.flow.cachedIn(viewModelScope)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当您使用LazyColumn中的项目列表时,您无法使用NULL检查跳过项目。尝试在您的“联系人”中显示一些东西。它可以是“加载”或默认值。
When you use list of items in LazyColumn you can't skip items with null check. Try show something in your "contact". It can be 'Loading' or default value.
我认为当我尝试在约束布局中使用AnimatedVisibily时,我会遇到同样的崩溃,这是一个错误
I think it's a BUG in compose that I get the same crash when I try to use AnimatedVisibility in ConstraintLayout layout, repeatedly controlling AnimatedVisibility to show and hide
如果您是HorizontalPager,请记住PAGERSTATE,请从Androidx.com.pose.found.pager导入它们,而不是com.google.accompanist:cancerist-page
If you youse HorizontalPager, rememberPagerstate, import them from androidx.compose.foundation.pager, not from com.google.accompanist:accompanist-page