im trying to use the geolocator package on flutter, ive read its documentation and accordingly inserted the codes
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
i stopped running the app and rerunned it. but it still says
E/flutter (10375): [ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: User denied permissions to access the device's location.
is there something im missing? any help appreciated
CodePudding user response:
Since version 8.0.0 of the geolocator plugin, the implicit requesting of permissions when calling the Geolocator.getCurrentPosition()
or Geolocator.getPositionStream()
methods have been removed. Starting from version 8.0.0 it is necessary to explicitly call the Geolocator.requestPermission()
method before calling the Geolocator.getCurrentPosition()
or Geolocator.getPositionStream()
methods.
The suggested flow looks something like this:
Future<Position> getLocation() {
// Test if location services are enabled.
bool serviceEnabled = await Geolocator.isLocationServiceEnabled();
if (!serviceEnabled) {
// Location services are not enabled don't continue
// accessing the position and request users of the
// App to enable the location services.
return Future.error('Location services are disabled.');
}
LocationPermission permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately.
return Future.error(Exception('Location permissions are permanently denied.'));
}
if (permission == LocationPermission.denied) {
// Permissions are denied, next time you could try
// requesting permissions again (this is also where
// Android's shouldShowRequestPermissionRationale
// returned true. According to Android guidelines
// your App should show an explanatory UI now.
return Future.error(Exception('Location permissions are denied.'));
}
}
// When we reach here, permissions are granted and we can
// continue accessing the position of the device.
return await Geolocator.getCurrentPosition();
}
CodePudding user response:
As stated above mentioned, you must update the requirements of implementation, you should ask for geolocation permission, and let the user allow it or not
please see the documentation : https://www.digitalocean.com/community/tutorials/flutter-geolocator-plugin