A value of type 'RenderObject?' can't be assigned to a variable of type 'RenderRepaintBoundary

Viewed 2554

This may be a simple question, but I couldn't find the answer. When I used this code:

RenderRepaintBoundary boundary =
                key.currentContext.findRenderObject();

I got error:

A value of type 'RenderObject?' can't be assigned to a variable of type 'RenderRepaintBoundary'.?

It's fine When I use dart version: 2.7.0, makes problem when I changed it to 2.12.0

2 Answers

To solve the error, just do this:

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

This way, you will cast it as a RenderRepaintBoundary.

To avoid The method 'findRenderObject' can't be unconditionally invoked because the receiver can be 'null'. error, add an exclamation mark to avoid null like this:

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