'RenderObject?' 类型的值不能分配给类型为“RenderRepaintBoundary”的变量

时间:2021-06-04 11:12:02

标签: flutter dart

这可能是一个简单的问题,但我找不到答案。当我使用此代码时:

RenderRepaintBoundary boundary =
                key.currentContext.findRenderObject();

我有错误:

<块引用>

“RenderObject?”类型的值不能分配给变量 输入'RenderRepaintBoundary'。?

没关系当我使用dart版本时:2.7.0,当我将其更改为2.12.0时会出现问题

2 个答案:

答案 0 :(得分:5)

要解决错误,只需执行以下操作:

RenderRepaintBoundary boundary =
            key.currentContext.findRenderObject() as RenderRepaintBoundary;

通过这种方式,您会将其转换为 RenderRepaintBoundary。

答案 1 :(得分:-1)

为了避免 The method 'findRenderObject' can't be unconditionally invoked because the receiver can be 'null'. 错误,添加一个感叹号以避免 null,如下所示:

RenderRepaintBoundary boundary = key.currentContext!.findRenderObject() as RenderRepaintBoundary;