getMyLocation function

Future<LatLng?> getMyLocation()

Returns current LatLng of the device.

If location service is disabled or denied, returns null. Else requests for permission and tries to return LatLng if permission granted.

Implementation

Future<LatLng?> getMyLocation() async {
  final _logger = AtSignLogger('getMyLocation');

  try {
    bool serviceEnabled;
    LocationPermission permission;

    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      return null;
    }

    permission = await Geolocator.checkPermission();
    if (permission == LocationPermission.denied) {
      permission = await Geolocator.requestPermission();
      if (permission == LocationPermission.deniedForever) {
        return null;
      }

      if (permission == LocationPermission.denied) {
        return null;
      }
    }

    if ((permission == LocationPermission.always) ||
        (permission == LocationPermission.whileInUse)) {
      var position = await Geolocator.getCurrentPosition();
      return LatLng(position.latitude, position.longitude);
    }

    return null;
  } catch (e) {
    _logger.severe('Error in getLocation $e');
    return null;
  }
}