checkLocationPermission method
Implementation
Future<bool> checkLocationPermission(BuildContext context) async {
bool serviceEnabled;
LocationPermission permission;
// Check if location services are enabled
serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
AppUtils.showSnackBar('Location services are disabled. Please enable them.');
await storageUtils.setLocationPermission('false');
return false;
}
// Check for location permissions
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied || permission == LocationPermission.deniedForever) {
permission = await Geolocator.requestPermission();
}
// Handle permission responses
if (permission == LocationPermission.denied) {
AppUtils.showSnackBar('Location permissions are denied. Please allow them.');
bool retry = await showPermissionDialog(context);
if (retry) {
permission = await Geolocator.requestPermission();
}
await storageUtils.setLocationPermission('false');
return false;
}
if (permission == LocationPermission.deniedForever) {
bool retry = await showPermissionDialog(context);
if (retry) {
permission = await Geolocator.requestPermission();
}
await storageUtils.setLocationPermission('false');
return false;
}
// If permission is granted
if (permission == LocationPermission.whileInUse || permission == LocationPermission.always) {
await _updateCurrentLocation(); // Fetch and update location
await storageUtils.setLocationPermission('true'); // Mark permission success
return true;
}
return false; // Fallback return
}