Flutter的RenderBox的dropChild不按预期工作。

huangapple go评论64阅读模式
英文:

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&lt;RenderBox, DynamicTimelineParentData&gt;,
        RenderBoxContainerDefaultsMixin&lt;RenderBox, DynamicTimelineParentData&gt; {
///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;
      }
    }
  }

huangapple
  • 本文由 发表于 2023年6月1日 03:41:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376779.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定