英文:
Flutter dropChild of RenderBox not working as expected
问题
I want to drop/delete a RenderBox in my class
class RenderDynamicTimeline extends RenderBox
with
ContainerRenderObjectMixin<RenderBox, DynamicTimelineParentData>,
RenderBoxContainerDefaultsMixin<RenderBox, DynamicTimelineParentData> {
///default Code
}
Here is my code for using dropChild()
:
void deleteChildBykey(Key? childKey) {
// Find the child with the matching key
for (final child in getChildrenAsList()) {
final dynamicTimelineParentData = child.parentData as DynamicTimelineParentData;
if (dynamicTimelineParentData.key == childKey) {
dynamicTimelineParentData.previousSibling = null;
dynamicTimelineParentData.nextSibling = null;
dropChild(child);
break;
}
}
}
Problem is, that I get a Null check operator used on a null value error
in the layout function from RenderDynamicTimeline
as I see through debugging, the child is already in the RenderDynamicTimeline as child there, but the parentData
is deleted.
The child is marked with NEEDS-PAINT DETACHED
How can I delete the child complete from the RenderDynamicTimeline
class?
英文:
I want to drop/delete a Renderbox in my class
class RenderDynamicTimeline extends RenderBox
with
ContainerRenderObjectMixin<RenderBox, DynamicTimelineParentData>,
RenderBoxContainerDefaultsMixin<RenderBox, DynamicTimelineParentData> {
///default Code
}
Here is my code for using dropChild()
:
void deleteChildBykey(Key? childKey) {
// Find the child with the matching key
for (final child in getChildrenAsList()) {
final dynamicTimelineParentData = child.parentData as DynamicTimelineParentData;
if (dynamicTimelineParentData.key == childKey) {
dynamicTimelineParentData.previousSibling = null;
dynamicTimelineParentData.nextSibling = null;
dropChild(child);
break;
}
}
}
Problem is, that I get a Null check operator used on a null value error
in the layout function from RenderDynamicTimeline
as I see through debugging, the child is already in the RenderDynamicTimeline as child there, but the parentData
is deleted.
The child is marked with NEEDS-PAINT DETACHED
How can I delete the child complete from the RenderDynamicTimeline
class?
答案1
得分: 1
如@pskink所提到的,remove
是解决方案:
void deleteChildBykey(Key? childKey) {
// 寻找具有匹配键的子项
for (final child in getChildrenAsList()) {
final dynamicTimelineParentData = child.parentData as DynamicTimelineParentData;
if (dynamicTimelineParentData.key == childKey) {
remove(child);
break;
}
}
}
英文:
as @pskink mentioned, remove
is the solution:
void deleteChildBykey(Key? childKey) {
// Find the child with the matching key
for (final child in getChildrenAsList()) {
final dynamicTimelineParentData = child.parentData as DynamicTimelineParentData;
if (dynamicTimelineParentData.key == childKey) {
remove(child);
break;
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论