Search code examples
iosflutterdartgeolocationflutter-dependencies

Flutter app crashes when requesting location on iOS


I am using the Geolocator plugin version 6.1.7.

When I try to request the current location, my app abruptly closes without any exception. Debug Console says Lost connection to device. Exited (sigterm)

This only happens on iOS (physical device and simulator). This function runs fine on the android simulator as well as a physical device.

Here's the code:

Future<bool> getLocation(bool refresh) async {
position = await Geolocator.getCurrentPosition();

if (mapPosition == null || refresh) {
  mapPosition = position;
}

return true;

}

Crashes at the getCurrentPosition() function.

I have added the required permissions in the Info.plist file.

<key>NSLocationWhenInUseUsageDescription</key>
<string>Your location is required to show you relevant meals in your area.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Your location is required to show you relevant meals in your area.</string>

Is there something I'm missing?

Edit:

I have made sure that the app has permission granted. The location's permission level is 'While Using the App' in the Settings. Also made sure my location is on in the Settings.


Solution

  • Answering my own question here.

    I explored the Info.plist to find any abnormalities and noticed that there was an item for Background Location updates.

    <key>EnableBackgroundLocationUpdates</key> <true/>

    I got rid of this line and the crashing problem went away.

    Hope this helps anyone else that experiences something similar.