0

The issue seems to be around calling a method with a _ before it, such as Future _showFirstPolylineMapMarkers

In my main.dart at the end of a method I call 2 entities, one is within main.dart and works fine, the other is in another Class, _mapItemsExample2.

...
       _sendIntermodalDataToSecondScreen(context, mappedValues, dest);       //works fine
       _mapItemsExample2?.getroute(deplat, deplong, deplocname);

      }
    });
  }

It will pass data to

  Future getroute(deplat, deplong, deplocname) async {
    print('getroutetest');
    _showFirstPolylineMapMarkers(deplat, deplong, deplocname); 
  }

  void _showFirstPolylineMapMarkers(deplat, deplong, deplocname) async {
...

But it goes dead and doesn't print or call _showFirstPolylineMapMarkers

If i replace .getroute... with ._showFirstPolylineMapMarkers(deplat, deplong, deplocname) to call the method directly it shows an error

The method '_showFirstPolylineMapMarkers' isn't defined for the type 'MapItemsExample2'.

If i remove the underscore at the start _ errors go away but it doesnt call it when ran

Any guidance appreciated

Thank you

al246
  • 220
  • 6
  • 16

1 Answers1

3

The _ indicates a private field or method, meaning that it can only be used within the file it's defined in.

It is a good practice to use the _ to make methods private to ensure they're only used where they are defined.

amo
  • 123
  • 3
  • Ah okay thanks for the explanation. I have removed the ```_``` concerned with this process and on code running it doesn't produce any results of ```print``` to show the the method has been called still – al246 Mar 22 '22 at 14:11
  • to call a public function from an other class, first need to make an object of this class – Javeed Ishaq Mar 22 '22 at 14:22
  • @JaveedIshaq Okay, how do I do that? Can't find a clear example on Google – al246 Mar 22 '22 at 14:25
  • see this [link](https://stackoverflow.com/questions/52549440/how-to-call-method-from-another-class-in-flutterdart) @al246 – amo Mar 22 '22 at 14:33
  • @amo Thanks I have seen that Q previously but the response was referencing an 'onTap' scenerio, is it the same principle? Thank you – al246 Mar 22 '22 at 14:34
  • https://www.w3adda.com/dart-tutorial/dart-object try looking into this if it might help :) – Javeed Ishaq Mar 22 '22 at 14:35