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

[亡魂溺海] 提交于 2020-12-15 02:58:31

问题


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?


回答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();


来源:https://stackoverflow.com/questions/61764390/how-to-handle-gps-location-provider-requires-access-fine-location-permission

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!