flutterlocation icon indicating copy to clipboard operation
flutterlocation copied to clipboard

getLocation does not complete on iOS devices on the first App start

Open Tyniann opened this issue 2 years ago • 14 comments

Description Flutter clean has been run. If you run an app that calls getLocation() on any iOS device (tested on different iPhones and iPads) for the first time it never completes. The same code works normally on android devices. I did the necessary checks first if the service is enabled and if the user gave permissions, then call the getLocation function.

Expected behavior The function completes and returns locationdata the first time.

Steps To Reproduce

1.) Start an iOS Simulator and make sure your app isn't already installed. If your app is already installed delete it and restart the device. 2.) Run your app and make sure getLocation() is executed. For example: LocationData locationData; Location location = Location(); locationData = await location.getLocation();

Tested on:

  • Android, API Level 31, simulator and real device. Works on both.
  • iOS, Version 16.2, simulator, iPad 12.9" 2nd gen, iPad 12.9" 6th gen, iPhone 11 pro max, iPhone 14 pro max

Other plugins:

Additional logs

Tyniann avatar Jan 18 '23 13:01 Tyniann

am also having this issue too

bensonarafat avatar Jan 23 '23 13:01 bensonarafat

am also having this issue too

bensonarafat avatar Jan 23 '23 13:01 bensonarafat

duplicate of #361

alidev0 avatar Jan 25 '23 12:01 alidev0

Still not working as it should. This is our current implementation/workaround.

setInitialLocation is used in the splash onLocationChanged is used on the map getLocation is used to "trick this package"

  @override
  Stream<LocationTrackingData> onLocationChanged() {
    if (Platform.isIOS) {
      getLocation();
    }
    return location.onLocationChanged.map((location) => LocationTrackingData(
          latitude: location.latitude,
          longitude: location.longitude,
          accuracy: location.accuracy,
          isMock: location.isMock ?? false,
        ));
  }

  @override
  Future<LocationTrackingData> getLocation() async {
    final location = await this.location.getLocation();
    return LocationTrackingData(
      latitude: location.latitude,
      longitude: location.longitude,
      accuracy: location.accuracy,
      isMock: location.isMock ?? false,
    );
  }

  @override
  Future<void> setInitialLocation() async {
    final status = await this.location.hasPermission();
    if (status != PermissionStatus.granted) return;
    final location = await onLocationChanged().first;
    final lat = location.latitude;
    final lon = location.longitude;
    if (lat == null || lon == null) return;
    _initialLocation = LatLonData(latitude: lat, longitude: lon);
  }

vanlooverenkoen avatar Feb 09 '23 10:02 vanlooverenkoen

Any Update on this i am also getting the same issue on IOS

Subrataporwal avatar Feb 28 '23 14:02 Subrataporwal

hi @Subrataporwal try geolocation this work fine for me https://pub.dev/packages/geolocator

bensonarafat avatar Feb 28 '23 14:02 bensonarafat

hi @Subrataporwal try geolocation this work fine for me https://pub.dev/packages/geolocator

Okay I am gonna try and update it here

Subrataporwal avatar Feb 28 '23 15:02 Subrataporwal

My app is getting rejected by Apple most probably because of this bug. getLocation is not completing/resolving. I have to stop using this plugin, looking at the number of issues, I think it's practically abandoned.

loolooii avatar Apr 09 '23 08:04 loolooii

My app is getting rejected by Apple most probably because of this bug. getLocation is not completing/resolving. I have to stop using this plugin, looking at the number of issues, I think it's practically abandoned.

Yeah. Same But I now use https://pub.dev/packages/geolocator

bensonarafat avatar Apr 09 '23 09:04 bensonarafat

Using Geolocator as well now and the review got past that point. So thanks for the suggestion @bensonarafat

loolooii avatar Apr 09 '23 17:04 loolooii

I'm facing the same issue.

angeloobeta avatar Apr 19 '23 00:04 angeloobeta

I'm also experiencing this issue

alliejc avatar Aug 06 '23 18:08 alliejc

@alliejc The workaround is to use https://pub.dev/packages/geolocator

angeloobeta avatar Aug 06 '23 20:08 angeloobeta

below (dirty) code works for me. (_lastLocationData might be set by an onLocationChanged listener active from elsewhere)

static Future<LocationData> getCurrentLocation() async {
  if (!await _checkOrRequestLocationPermissions()) {
    throw Exception("Location permissions not granted");
  }

  for (int retryCount = 0; retryCount < 2; retryCount++) {
    try {
      if (_lastLocationData != null) {
        return _lastLocationData!;
      }

      final completer = Completer<LocationData>();

      StreamSubscription<LocationData>? locationChangedSubscription;
      try {
        locationChangedSubscription = _location.onLocationChanged.listen((locationData) {
          if (!completer.isCompleted) {
            completer.complete(locationData);
          }
        });

        final locationData = await Future.any([
          _location.getLocation(),
          completer.future,
        ]).timeout(timeoutDuration);

        return locationData;
      } finally {
        locationChangedSubscription?.cancel();
      }
    } catch (e) {}
  }

  throw Exception("Failed to get location within retry limit");
}

lcampanella98 avatar Apr 25 '24 04:04 lcampanella98