Flutter geolocator权限

3

我的代码是:

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

我在Android清单文件中添加了以下内容:

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

安卓模拟器,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中说明:

当您尝试通过getCurrentPosition或getPositionStream方法获取位置时,地理定位器将自动尝试请求权限。但是,我们提供了一些方法,可以让您手动处理请求权限。

权限不应该作为权限请求中的选择结果来设置吗?

我在iOS上也遇到了几乎相同的问题:

在“设置-隐私-位置服务-我的应用程序权限设置为使用期间”。尝试访问地理位置后,我收到了对话框窗口而不是错误:

[VERBOSE-2:ui_dart_state.cc(209)]未处理异常:用户拒绝访问设备的位置。 #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)

但是为什么?


1
你需要向用户请求实际的弹窗权限。仅在清单文件中添加权限是不够的。Android从不自动请求权限,这是一个手动过程(或者有时库会代表你请求以避免编写代码)。 - Mariano Zorrilla
我建议使用permission_handler包来获取用户的许可。 - Peter Koltai
2个回答

10

虽然我晚了,但这对其他开发人员可能会有帮助。

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检查,当您请求权限时,它会自动要求用户启用设备位置服务。


我删除了isServiceEnable检查。现在它会要求权限。你是救命恩人。谢谢。 - Umit Kırtıl
1
2022年9月了,我仍然有这个问题。 - João Martins

3

作为初学者,你可以尝试这段代码:

void getLocation() async {
    LocationPermission permission;
    permission = await Geolocator.checkPermission();
    permission = await Geolocator.requestPermission();
    if( permission== LocationPermission.denied){
         //nothing
    }
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
    print(position);
  }

但更好的格式是使用Future和async:

Future<Position> determinePosition() async {
    bool serviceEnabled;
    LocationPermission permission;

    serviceEnabled = await Geolocator.isLocationServiceEnabled();
    if (!serviceEnabled) {
      return Future.error('Location services are disabled.');
    }

    permission = await Geolocator.checkPermission();
    permission = await Geolocator.requestPermission();

      if (permission == LocationPermission.denied) {
        return Future.error('Location permissions are denied');
      }


    if (permission == LocationPermission.deniedForever) {
      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }
    Position position = await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.low);
    print(position);

    return await Geolocator.getCurrentPosition();
  }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接