Search code examples
flutterdartlocation

LateInitializationError: Field 'currentLatLng' has not been initialized in Flutter


I am displaying my current location on google maps using this library: package:location/location.dart. Pub Dev Link

The problem is whenever I open the app in release mode it crashes and shows:

LateInitializationError: Field 'currentLatLng' has not been initialized

It does not crash in debug mode on an Android Device. It does, however, crash on iOS (release and debug mode).

I am not sure what I am doing wrong. Here's my widget and attempt:

    class TestGoogle extends StatefulWidget {
      const TestGoogle({Key? key}) : super(key: key);
      @override
      _TestGoogleState createState() => _TestGoogleState();
    }

    class _TestGoogleState extends State<TestGoogle> {
      late LatLng currentLatLng;
      late GoogleMapController mapController;
      Location location = new Location();
      var latitude;
      var longitude;
      late LocationData _locationData;
      get() async {
        _locationData = await location.getLocation();
        latitude = _locationData.latitude;
        longitude = _locationData.longitude;
        setState(() {
          currentLatLng = new LatLng(latitude, longitude);
        });
      }
      @override
      initState() {
        super.initState();
        get();
      }
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: GoogleMap(
            myLocationEnabled: true,
            onCameraMove: (CameraPosition cameraPosition) {
              print(cameraPosition.zoom);
            },
            initialCameraPosition:
                CameraPosition(target: currentLatLng, zoom: 15.0),
          ),
        );
      }
    }

Solution

  • The below codes should fix your problem. Your problem reason, try use variable before initializing.

    
    class TestGoogle extends StatelessWidget {
      TestGoogle({Key? key}) : super(key: key);
    
      late GoogleMapController mapController;
      Location location = Location();
    
      Future<LatLng> get() async {
        final _locationData = await location.getLocation();
        return LatLng(_locationData.latitude, _locationData.longitude);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: FutureBuilder<LatLng>(
            future: get(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                final locationModel = snapshot.data!;
                final latitude = locationModel.latitude;
                final longitude = locationModel.longitude;
    
                return GoogleMap(
                  myLocationEnabled: true,
                  onCameraMove: (CameraPosition cameraPosition) {
                    print(cameraPosition.zoom);
                  },
                  initialCameraPosition: CameraPosition(target: locationModel, zoom: 15.0),
                );
              }
              return const CircularProgressIndicator();
            },
          ),
        );
      }
    }