Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fetching Current Location with Permission not working

I am using geolocator: '^3.0.1' permission_handler: '^3.0.0'

Now I want to fetch the current location of the user and show it on the map as the user opens the Map.

So my Code is :

Future<void> requestPermission() async {
    PermissionHandler()
        .checkPermissionStatus(PermissionGroup.location)
        .then((PermissionStatus permissionStatus) async {
      print("Checking Permission " + permissionStatus.toString());
      if (permissionStatus == PermissionStatus.granted) {
        _getCurrentLocation();
      } else {
        print("Asking Permission " + permissionStatus.toString());

        final List<PermissionGroup> permissions = <PermissionGroup>[
          PermissionGroup.locationWhenInUse
        ];
        final Map<PermissionGroup, PermissionStatus> permissionRequestResult =
            await PermissionHandler().requestPermissions(permissions);

        if (PermissionStatus.granted ==
            permissionRequestResult[PermissionGroup.locationWhenInUse]) {
          print("Permission Granted " + permissionStatus.toString());

          _getCurrentLocation();
        }
      }
    });
  }

and permissions are defined in the manifest for android and info.list for IOS.

Now the issue is when I run this function and when it calls requestPermission function, it shows the popup asking for the permission and

when I allow the permission app crashes with an error :

java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions: ... java.lang.IllegalStateException: Reply already submitted

and also the result of permission is Permission.disabled though I allowed the permission in application settings and in permission it shows that location is permission is allowed. but I tried opening app several times it shows Permission.disabled.

and even if I deny the app crashes with the same error.

So what I have concluded is :

If I allow or deny it crashes because it is requesting multiple times and even if I allow the result is Permission.disabled.

Link for the video: https://youtu.be/A1DKkw6u4HI

Can anyone help me solving this issue?

Or please tell me how to take the current location map easily

like image 852
CodeGeek Avatar asked Mar 23 '19 09:03

CodeGeek


People also ask

Which permissions are required to get a location in Android?

Android offers two location permissions: ACCESS_COARSE_LOCATION and ACCESS_FINE_LOCATION .


1 Answers

please tell me how to take the current location map easily

If you just need to fetch current location easily and you just need location permission then :

you can use location plugin with flutter :

In your pubspec.yml : location : ^2.3.0

Then for fetching Current location :

Import location Package

 import 'package:location/location.dart' as locationPackage;

Add this to your State

 locationPackage.Location _locationService = new locationPackage.Location();
     bool _permission = false;

Call this function in initState or whenever you need current Location

fetchCurrentLocation() async {
    await _locationService.changeSettings(
        accuracy: locationPackage.LocationAccuracy.HIGH, interval: 1000);

    locationPackage.LocationData location;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      bool serviceStatus = await _locationService.serviceEnabled();
      print("Service status: $serviceStatus");
      if (serviceStatus) {
        _permission = await _locationService.requestPermission();
        print("Permission: $_permission");
        if (_permission) {
          location = await _locationService.getLocation();

          print("Location: ${location.latitude}");
        }
      } else {
        bool serviceStatusResult = await _locationService.requestService();
        print("Service status activated after request: $serviceStatusResult");
        if (serviceStatusResult) {
          fetchCurrentLocation();
        }
      }
    } on PlatformException catch (e) {
      print(e);
      if (e.code == 'PERMISSION_DENIED') {
        //error = e.message;
      } else if (e.code == 'SERVICE_STATUS_ERROR') {
        //error = e.message;
      }
      location = null;
    }
  }
like image 134
Abubakker Moallim Avatar answered Nov 15 '22 06:11

Abubakker Moallim