Flutterlocation: [iOS] location.getLocation never returns

Created on 1 Apr 2020  路  13Comments  路  Source: Lyokone/flutterlocation

Describe the bug
Future location.getLocation() never returns

Expected behavior
return result(any result)

Tested on:

  • iOS, Version 13.3.1, iPhone 6s

Other plugins:

  • No other pugins

Steps to reproduce:

  1. Turn off location services.
  2. Open the app
  3. Try to get location
  4. Go to settings and enable location services
  5. Back to app and try to get location. getLocation never returns!

Example code:
Create new flutter project and add location to pub spec. Then use this code in main.dart

import 'package:flutter/material.dart';
import 'package:location/location.dart';

void main() {
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Location location = Location();
  bool _serviceEnabled;
  PermissionStatus _permissionGranted;
  LocationData _locationData;

  void _getLocation() async {
    print('Check if service is enabled');
    _serviceEnabled = await location.serviceEnabled();
    if (!_serviceEnabled) {
      _serviceEnabled = await location.requestService();
      if (!_serviceEnabled) {
        return;
      }
    }

    print('Has permissions');
    _permissionGranted = await location.hasPermission();
    if (_permissionGranted == PermissionStatus.denied) {
      _permissionGranted = await location.requestPermission();
      if (_permissionGranted != PermissionStatus.granted) {
        return;
      }
    }

    print('Try to get location');
    _locationData = await location.getLocation();
    print(_locationData);
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              'Location:',
            ),
            Text(
              _locationData != null
                  ? '${_locationData.toString()}'
                  : 'no location',
              style: Theme.of(context).textTheme.headline6,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _getLocation,
        child: Icon(Icons.location_on),
      ),
    );
  }
}

Most helpful comment

Hi all, I believe that your Simulators aren't working as expected due to there being no location set for it to use. I've opened #358 which adds steps on how to do this to the README :)

All 13 comments

for iPhone device worked as expected but not the simulator

We tested on two devices in the office, iPhone 6s and iPhone X and it had same issue. Found a workaround for now - using location_permissions plugin and once the user gives access, we use flutter location plugin to get location.

Same not working on Simulator even after permission is enabled. The issue came after updating the Xcode to 11.4

We tested on two devices in the office, iPhone 6s and iPhone X and it had same issue. Found a workaround for now - using location_permissions plugin and once the user gives access, we use flutter location plugin to get location.

@yokoboko I tried your solution - first location_permissions to get access, and then location to get the location. Still no luck. It never returns at
locationData = await _location.getLocation();
Can you share any working code?

@theolof Don't instantiate _location before you get access from location_permission plugin.

Working example(removed some code for showing alerts, look at ' // Get coordinates'):

import 'package:location/location.dart' as loc;
import 'package:location_permissions/location_permissions.dart' as locperm;

.....

void _useMyCurrentLocationAction() async {

    // Locatoin services enabled
    locperm.ServiceStatus serviceStatus =
        await locperm.LocationPermissions().checkServiceStatus();
    if (serviceStatus != locperm.ServiceStatus.enabled) {
      // show message to the user
      return;
    }

    // Permission granted
    locperm.PermissionStatus permissionGranted =
        await locperm.LocationPermissions().checkPermissionStatus();
    if (permissionGranted != locperm.PermissionStatus.granted) {
      permissionGranted = await locperm.LocationPermissions()
          .requestPermissions(
              permissionLevel:
                  locperm.LocationPermissionLevel.locationWhenInUse);
      if (permissionGranted != locperm.PermissionStatus.granted) {
        // show message to the user
        return;
      }
    }

    // Get coordinates
    if (_location == null) _location = loc.Location();
      try {
        final currentLocation = await _location.getLocation();
        // you are good to go
      } on PlatformException catch (e) {
        if (e.code == 'PERMISSION_DENIED') {
          // show message
        } else {
          // show error
        }
      }
  }

Thanks, but still no luck, at least not with the simulator.
I even copied all your code and tried it. It still hangs on
final currentLocation = await _location.getLocation();

Is it working for you in the simulator as well as on real devices?

@theolof Never tried it on a simulator. I always test on real device.

We have the exact same problem here. It does not work on my Simulator (Version 11.4.1), but it works on my friend's Simulator (Version 11.3.1). Maybe that is the problem.

It works for me on my real device.
I didn't have to use the workaround, it worked anyway.

Hi all, I believe that your Simulators aren't working as expected due to there being no location set for it to use. I've opened #358 which adds steps on how to do this to the README :)

You are a savior @MichaelM97 ! This solved my problem! I could not wrap my head around why it worked on my real device but not the simulator, especially when it had been working earlier.

I use real iPhone 6S and getLocation never returns location.

I don't get why this was closed.
The issue ocur in real hardware too.
(iOS 13)
Steps to reproduce:

  1. Disable Location Services
  2. Open the App and instantiate Location()
  3. Check permissions or try to check if service is enabled.
  4. Enable the service.
  5. Go back to App
    (Don't matter if the app has permission or you are giving permissions now)
  6. Try to get location
  7. The method NEVER returns.
Was this page helpful?
0 / 5 - 0 ratings