getCurrentPosition static method

Future<LatLong?> getCurrentPosition()

Returns the current coordinates when permission is granted.

The method falls back to the last known position when the live request fails.

Implementation

static Future<LatLong?> getCurrentPosition() async {
  final granted = await ensurePermission();

  if (!granted) return null;

  final lastKnown = await Geolocator.getLastKnownPosition();

  try {
    final current = await Geolocator.getCurrentPosition(
      locationSettings: const LocationSettings(
        accuracy: LocationAccuracy.high,
      ),
    );
    return LatLong(current.latitude, current.longitude);
  } on Exception catch (_) {
    if (lastKnown == null) return null;
    return LatLong(lastKnown.latitude, lastKnown.longitude);
  }
}