Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

flutter

dart

 void getPositions() {
  final RenderBox renderBox = scheduleForDayKey.currentContext!.findRenderObject();
  final position = renderBox.localToGlobal(Offset.zero);
  print("POSITION of Red: $position ");
}

This part is underlined with red:

scheduleForDayKey.currentContext!.findRenderObject()

When I hover, it tells me the error:

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

Maybe this section of code is outdated or something because I see a lot of similar code examples on the internet, which for some reason don't work for me.

like image 775
Vlad-Sebastian Cretu Avatar asked May 15 '21 19:05

Vlad-Sebastian Cretu


People also ask

How to assign a value of type type'type'to a renderbox?

A value of type 'Type' can't be assigned to a variable of type 'RenderBox'. Sorry, something went wrong. A value of type 'Type' can't be assigned to a variable of type 'RenderBox'. It should be RenderBox box = key.currentContext!.findRenderObject () as RenderBox;

How to fix renderbox variable is not working?

As the error itself says Try changing the type of the variable, or casting the right-hand type to 'RenderBox'. like mentioned above, should resolve your issue. Closing this as resolved.

How to assign a value to a variable of type'renderbox'?

A value of type 'Type' can't be assigned to a variable of type 'RenderBox'. It should be RenderBox box = key.currentContext!.findRenderObject () as RenderBox;


2 Answers

Try writing:

final RenderBox renderBox = scheduleForDayKey.currentContext!
                                             .findRenderObject() as RenderBox;
like image 163
Smarak Das Avatar answered Nov 11 '22 05:11

Smarak Das


for me this code work with my for sdk: ">=2.12.0 <3.0.0"

final RenderBox box = context.findRenderObject() as RenderBox;

like image 25
Apps Maker Avatar answered Nov 11 '22 06:11

Apps Maker