-1

I am trying to get the position of a user. I am using GeoLocator and google_maps_flutter dependencies to do this.

When building I get an error for a split second when switching from the login screen to the google map screen. The error I am seeing in the console is...

The following NoSuchMethodError was thrown building googleMaps(dirty, state: MapAppState#bc232):
The getter 'latitude' was called on null.
Receiver: null
Tried calling: latitude

My code is..

class googleMaps extends StatefulWidget {
  @override
  MapAppState createState() => MapAppState();
}

class MapAppState extends State<googleMaps> {
  GoogleMapController mapController;
  Position position;
  static LatLng _initialPosition;
  static LatLng _lastMapPosition = _initialPosition;

  void _onMapCreated(GoogleMapController controller) {
    mapController = controller;
  }

  void getLocation() async {
    position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);
    setState(() {
      _initialPosition = LatLng(position.latitude, position.longitude);
    });
  }

  @override
  Widget build(BuildContext context) {
    getLocation();
    return MaterialApp(
      home: Scaffold(
        body: GoogleMap(
          onMapCreated: _onMapCreated,
          initialCameraPosition: CameraPosition(
            target: LatLng(position.latitude, position.longitude),
            zoom: 11.0,
          ),
          gestureRecognizers: <Factory<OneSequenceGestureRecognizer>>[
            new Factory<OneSequenceGestureRecognizer>(
              () => new EagerGestureRecognizer(),
            ),
          ].toSet(),
          cameraTargetBounds: CameraTargetBounds.unbounded,
          minMaxZoomPreference: MinMaxZoomPreference.unbounded,
          rotateGesturesEnabled: true,
          scrollGesturesEnabled: true,
          zoomGesturesEnabled: true,
          tiltGesturesEnabled: true,
          myLocationButtonEnabled: true,
          myLocationEnabled: true,
        ),
      ),
    );
  }
}

I seen someone suggested using FutureBuilder but I do not believe I would like to take this route for fixing the error. Could someone point me in the right direction for fixing this error?

Thanks in advance!

TylerB56
  • 40
  • 8
  • "but I do not believe I would like to take this route" Programming isn't based on faith. Someone suggested you a fix, you should try it and see if it works. – nvoigt Nov 30 '20 at 09:01
  • 1
    Does this answer your question? [What is a Future and how do I use it?](https://stackoverflow.com/questions/63017280/what-is-a-future-and-how-do-i-use-it) – nvoigt Nov 30 '20 at 09:02

2 Answers2

1

When you call getLocation() its not being called in a blocking manor. So when the GoogleMap widget is being created the location hasnt been recieved by the flutter event loop. A quick fix for this is to call await before getLocation(). Unfortunately this will make the screen load time slower. A more permenent fix would be setting _initalPosition to a predertemined LatLng or saving the users last position in sharedPreferances and setting it to that value to start with. That way the UI still loads quickly and the setState() call can update the UI later.

0

Put getLocation() at initState

faithomotoso
  • 385
  • 2
  • 10