getCurrentLocation method

Future<Position> getCurrentLocation()

Retrieves the user's current GPS position.

This method:

  • Checks whether location services are enabled.
  • Verifies and requests permissions if necessary.
  • Returns a Position with high accuracy.

Throws:

Example:

final position = await GeolocatorService().getCurrentLocation();

Implementation

Future<Position> getCurrentLocation() async {
  bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
  if (!serviceEnabled) throw LocationServicesDisabledException();

  LocationPermission permission = await Geolocator.checkPermission();
  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
      throw LocationPermissionDeniedException();
    }
  }

  if (permission == LocationPermission.deniedForever) {
    throw LocationPermissionPermanentlyDeniedException();
  }

  return await Geolocator.getCurrentPosition(
    locationSettings: const LocationSettings(accuracy: LocationAccuracy.best),
  );
}