Ad

The Method 'globalToLocal' Isn't Defined For The Type 'RenderObject'

- 1 answer

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
Ad

Answer

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
Ad
source: stackoverflow.com
Ad