determinePosition method
Future<Position?>
determinePosition(
{ - bool requestPermission = false,
- bool showLoader = true,
- bool openAppSetting = true,
})
Implementation
Future<Position?> determinePosition({
bool requestPermission = false,
bool showLoader = true,
bool openAppSetting = true,
}) async {
LocationPermission permission;
Position? position;
permission = await Geolocator.checkPermission();
if (permission == LocationPermission.deniedForever) {
if (openAppSetting) {
Geolocator.openAppSettings();
}
} else if (permission == LocationPermission.denied && requestPermission) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.deniedForever) {
if (openAppSetting) {
Geolocator.openAppSettings();
}
} else if (permission == LocationPermission.always ||
permission == LocationPermission.whileInUse) {
if(showLoader) {
LoadingUtils.showLoader();
}
final location = lp.Location();
bool enabled = await location.serviceEnabled();
if (!enabled) {
enabled = await location.requestService();
}
if (enabled) {
position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
} else {
// Fluttertoast.showToast(
// msg: ErrorMessages.locationServiceAreNotEnabled);
}
LoadingUtils.hideLoader();
}
} else if (permission == LocationPermission.always ||
permission == LocationPermission.whileInUse) {
if (showLoader) {
LoadingUtils.showLoader();
}
final location = lp.Location();
bool enabled = await location.serviceEnabled();
if (!enabled) {
enabled = await location.requestService();
}
if (enabled) {
position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);
} else {
// Fluttertoast.showToast(msg: ErrorMessages.locationServiceAreNotEnabled);
}
LoadingUtils.hideLoader();
}
return position;
}