Search code examples
flutterlocationgoogle-maps-flutter

How to handle "gps" location provider requires ACCESS_FINE_LOCATION permission. in flutter apps


Hi I am trying to get user location by using location package and google-maps-flutter in flutter.. but I get an error that say "gps" location provider requires ACCESS_FINE_LOCATION permission. I have added this code in my androidmanifest file

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

I still get an error although I have added that code..is there something that I should do to prevent this problem?


Solution

  • You should check your self permission status and If it did not granted by user, you should request for permission. You could use location plugin for request permissions.

    https://pub.dev/packages/location

    Location location = new Location();
    
    bool _serviceEnabled;
    PermissionStatus _permissionGranted;
    LocationData _locationData;
    
    _serviceEnabled = await location.serviceEnabled();
    if (!_serviceEnabled) {
      _serviceEnabled = await location.requestService();
      if (!_serviceEnabled) {
        return;
      }
    }
    
    _permissionGranted = await location.hasPermission();
    if (_permissionGranted == PermissionStatus.denied) {
      _permissionGranted = await location.requestPermission();
      if (_permissionGranted != PermissionStatus.granted) {
        return;
      }
    }
    
    _locationData = await location.getLocation();