flutterdartgeolocator

Fixing a flutter lint for late variable


I have the following flutter dart code which uses the geolocator package.

Position? _position;
  void _getCurrentLocation() async {

    showDialog(
        context: context,
        builder: (context) {
          return const Center(child: CircularProgressIndicator());
        });
    Position position = await _determinePosition();

    if (!mounted) return;
    setState(() {
      _position = position;

      lat = position.latitude.toString();
      long = position.longitude.toString();
}

I am getting a lint warning (see below) on _position and it is greyed out saying that "The value of the field '_position' isn't used. (Documentation) Try removing the field, or using it."

Position? _position;

If I comment the line out I get an error under setState for _position = position saying Undefined name '_position'.

The code works and I don't know if its best to just ignore the lint or if there is a better way to fix it?


Solution

  • Even though you have assigned the value, the Dart analyzer knows that you haven't use it. Start using the value somewhere else, maybe in a Text widget, and the warning will be gone.