'Null check operator used on a null value when value is declared and assigned

class _HomePageState extends State<HomePage> {
  LatLng? _pickUp;

  void getCurrentLocation() async {
    LocationPermission permission = await Geolocator.requestPermission();

    final geoposition = await Geolocator.getCurrentPosition(
        desiredAccuracy: LocationAccuracy.high);

    setState(() {
      _pickUp = LatLng(geoposition.latitude, geoposition.longitude);
    });
  }
  initState() {
    super.initState();
    getCurrentLocation();
  }
}

_pickUp is assigned in the getCurrentLocation function which is called in the initState(), but when I use _pickUp! later in my widget, null error is shown. May I know how I can edit my code so that _pickUp is assigned.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source