如何在Android中添加地理定位器的权限?



所以我有以下错误:

√  Built buildappoutputsflutter-apkapp-debug.apk.
D/FlutterLocationService( 6191): Creating service.
D/FlutterLocationService( 6191): Binding to location service.
Lost connection to device.

我也不知道为什么上面写着D/FlutterLocationService,而不是D/FlutterGeolocator.

我听说你现在需要手动请求许可,但不知道怎么做。我该如何将地理定位器权限的代码添加到此代码中?

AppStateProvider() {
_saveDeviceToken();
FirebaseMessaging.onMessage;
FirebaseMessaging.onMessageOpenedApp;
_setCustomMapPin();
_getUserLocation();
_listemToDrivers();
Geolocator.getPositionStream().listen(_updatePosition);

}
// ANCHOR: MAPS & LOCATION METHODS
_updatePosition(Position newPosition) {
position = newPosition;
notifyListeners();
}
Future<Position> _getUserLocation() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
position = await Geolocator.getCurrentPosition();
List<Placemark> placemark =
await placemarkFromCoordinates(position.latitude, position.longitude);

您只需在获取用户位置之前添加权限请求在_getUserLocation()中,

之前
position = await Geolocator.getCurrentPosition();

添加下面的代码

final permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
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('Location permissions are denied');
}
}
if (permission == LocationPermission.deniedForever) {
// Permissions are denied forever, handle appropriately. 
return Future.error(
'Location permissions are permanently denied, we cannot request permissions.');
} 

你也可以查看https://pub.dev/packages/geolocator

最新更新