gpt4 book ai didi

Flutter 地理定位器权限

转载 作者:行者123 更新时间:2023-12-05 08:36:05 25 4
gpt4 key购买 nike

我的代码是:

class _LoadingScreenState extends State<LoadingScreen> {
void getLocation() async {
Position position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.low);
print(position);
}

我在 Android list 中添加了以下内容:

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

Android 模拟器,GPS 开启。

尝试访问地理定位后,我收到错误消息:

E/flutter ( 5034): [ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: User denied permissions to access the device's location.
E/flutter ( 5034): #0 MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:127:7)
E/flutter ( 5034): <asynchronous suspension>
E/flutter ( 5034): #1 _LoadingScreenState.getLocation (package:clima/screens/loading_screen.dart:11:25)
E/flutter ( 5034): <asynchronous suspension>

设置 - 安全和位置 - 隐私 - 位置 - 应用级别权限中,我的应用权限已关闭。

打开它后我收到一个请求:

enter image description here

为什么会这样?

在这里https://pub.dev/packages/geolocator据称

The geolocator will automatically try to request permissions when youtry to acquire a location through the getCurrentPosition orgetPositionStream methods. We do however provide methods that willallow you to manually handle requesting permissions.

难道权限请求中的选择不应该设置权限吗?

我在 iOS 上遇到了几乎相同的问题:在设置-隐私-定位服务-我的应用程序权限设置为使用时。尝试访问地理位置后,我收到对话框窗口但出现错误:

[VERBOSE-2:ui_dart_state.cc(209)] Unhandled Exception: User deniedpermissions to access the device's location.#0 MethodChannelGeolocator.getCurrentPosition (package:geolocator_platform_interface/src/implementations/method_channel_geolocator.dart:127:7)#1 _LoadingScreenState.getLocation (package:clima/screens/loading_screen.dart:11:25)

但为什么呢?

最佳答案

我来晚了,但它对其他开发人员很有用。

bool serviceEnabled;
LocationPermission permission;

// Test if location services are enabled.
serviceEnabled = await Geolocator.isLocationServiceEnabled();


permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
Get.snackbar('', 'Location Permission 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.');
}
return Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.high);

只需去掉isServiceEnabled检查,当你请求权限时,它会自动要求用户启用设备定位服务。

关于Flutter 地理定位器权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70743435/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com