Home > Software engineering >  Flutter: geolocator Error? Warning? [CoreLocation] This method can cause UI unresponsiveness if invo
Flutter: geolocator Error? Warning? [CoreLocation] This method can cause UI unresponsiveness if invo

Time:02-02

I am developing in Flutter and am trying to get GPS information using the geolocator package.

I was able to get GPS information on the emulator with no problem, but when I started the app on the actual device, I could not get GPS. The app was working fine, so it was not an error, but there was a message.

"This method can cause UI unresponsiveness if invoked on the main thread. Instead, consider waiting for the -locationMana gerDidChangeAuthorization: callback and checking authorizationStatus first."

Code on Flutter side

Future<Position> getGps(BuildContext context) async {
    return await Geolocator.getCurrentPosition(
      desiredAccuracy: LocationAccuracy.high,
      timeLimit: Duration(seconds: 5),
    );
}

(WARNING)Code on IOS side

    - (ServiceStatus)checkServiceStatus:(PermissionGroup)permission {
        return [CLLocationManager locationServicesEnabled] ? ServiceSttus Enabled
            : ServiceStatusDisabled;
    }

2023/1/26 added

I had added it to info.plist. Still, I am not getting GPS information.

info.plist

<key>NSLocationAlwaysUsageDescription</key>
<string>Access location information to get a location</string>.
<key>NSLocationWhenInUseUsageDescription</key>
<string>Access location information to get location</string>

How do I get GPS information? Permission to acquire location services is granted.

iOS:16.2 xcode: 14 geolocator: 7.7.1

2023/1/26 added

Current Status

Emulator(Android) ...〇(GPS information has been acquired.)

Emulator(iOS(iphone SE2)) ...〇(GPS information has been acquired.)

Actual Device(iOS(iphone SE2)) ...× (GPS cannot be acquired)

CodePudding user response:

Try this

  1. Add "location_permissions: 3.0.0 1" this dependencies in "pubspec.yaml". Please note that I did that for flutter 1.22.0 so for flutter 2.0 this might be an issue.

  2. Import the package in the file

    import 'package:location_permissions/location_permissions.dart';
    
  3. Add the following code on the page where you want to ask for permission. (Better to add that on the very first page of your app.)

    @override
       void initState() {
        ....
       if (Platform.isIOS) {
         location_permission();
       }
       ....
    }
    
  4. Add the following two methods in the same file

      void location_permission() async {
      final PermissionStatus permission = await _getLocationPermission();
      if (permission == PermissionStatus.granted) {
        final position = await geolocator.getCurrentPosition(
            desiredAccuracy: LocationAccuracy.best);
    
        // Use the position to do whatever...
      }
     }
    
     Future<PermissionStatus> _getLocationPermission() async {
      final PermissionStatus permission = await LocationPermissions()
          .checkPermissionStatus(level: LocationPermissionLevel.location);
    
      if (permission != PermissionStatus.granted) {
        final PermissionStatus permissionStatus = await LocationPermissions()
            .requestPermissions(
                permissionLevel: LocationPermissionLevel.location);
    
        return permissionStatus;
      } else {
        return permission;
      }
     }
    

CodePudding user response:

I was able to solve my problem. I thought it was a permissions issue in my case, but it was just a location acquisition timeout. I modified the location acquisition timeout from 5 seconds to 20 seconds and was able to get the necessary information without any problems.

The warnings are still there, but we decided not to worry about them since we were able to solve the problem.

return await Geolocator.getCurrentPosition(
  desiredAccuracy: LocationAccuracy.high,
  timeLimit: Duration(seconds: 20),
);
  • Related