getUserLocationPosition function

Future<Position> getUserLocationPosition()

Implementation

Future<Position> getUserLocationPosition() async {
  bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
  LocationPermission permission = await Geolocator.checkPermission();
  if (!serviceEnabled) {
    //
  }

  if (permission == LocationPermission.denied) {
    permission = await Geolocator.requestPermission();
    if (permission == LocationPermission.denied) {
      await Geolocator.openAppSettings();
      throw language.locationPermissionIsDenied;
    }
  }

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

  return await Geolocator.getCurrentPosition(
          desiredAccuracy: LocationAccuracy.high)
      .then((value) {
    return value;
  }).catchError((e) async {
    return await Geolocator.getLastKnownPosition().then((value) async {
      if (value != null) {
        return value;
      } else {
        throw language.enableLocation;
      }
    }).catchError((e) {
      toast(e.toString());
    });
  });
}