Home > Enterprise >  The method 'globalToLocal' isn't defined for the type 'RenderObject'
The method 'globalToLocal' isn't defined for the type 'RenderObject'

Time:03-24

How to fix this. Error message: The method 'globalToLocal' isn't defined for the type 'RenderObject'. Try correcting the name to the name of an existing method, or defining a method named 'globalToLocal'.

enter image description here

My Code

@override
  Widget build(BuildContext context) {            
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: GestureDetector(
          onPanUpdate: (details) {            
            setState(() {
              RenderObject? renderBox = context.findRenderObject();
              points.add(TouchPoints(
                  points: renderBox?.globalToLocal(details.globalPosition),//ERROR

CodePudding user response:

You have to cast the object like so:

@override
  Widget build(BuildContext context) {            
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        body: GestureDetector(
          onPanUpdate: (details) {            
            setState(() {

              //Here at the end and at the begining of the line
              RenderBox? renderBox = context.findRenderObject() as RenderBox; 

              points.add(TouchPoints(
                  points: renderBox?.globalToLocal(details.globalPosition),//ERROR
  • Related