Flutter dropChild of RenderBox not working as expected

39 views Asked by At

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

There are 1 answers

0
Lukas Fürst On

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;
      }
    }
  }