RenderBox的Flutter dropChild未按预期工作

krugob8w  于 2023-06-07  发布在  Flutter
关注(0)|答案(1)|浏览(119)

我想删除我的类中的渲染框

class RenderDynamicTimeline extends RenderBox
    with
        ContainerRenderObjectMixin<RenderBox, DynamicTimelineParentData>,
        RenderBoxContainerDefaultsMixin<RenderBox, DynamicTimelineParentData> {
///default Code
}

下面是使用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;
      }
    }
  }

问题是,我在layout函数中从RenderDynamicTimeline得到了一个Null check operator used on a null value error,正如我通过调试看到的那样,子对象已经在RenderDynamicTimeline中作为子对象存在,但是parentData被删除了。子项标记为NEEDS-PAINT DETACHED
如何从RenderDynamicTimeline类中删除完整的子类?

u3r8eeie

u3r8eeie1#

正如@pskink提到的,remove是解决方案:

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

相关问题