0

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?

wahyu
  • 1,679
  • 5
  • 35
  • 73

1 Answers1

1

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();
Savas Adar
  • 4,083
  • 3
  • 46
  • 54
  • Hi.. I try your code but I get this message in my console ```There is no constant named 'granted' in 'PermissionStatus'. Try correcting the name to the name of an existing constant, or defining a constant named 'granted'.``` – wahyu May 13 '20 at 02:33
  • is there something that I should add? – wahyu May 13 '20 at 02:33
  • I change ```denied``` and ```granted``` into caps lock and it works.. thank you for your help – wahyu May 13 '20 at 10:26