Geolocator:返回错误时,应用程序不会请求位置权限。但如果没有返回错误,它会请求位置权限

问题描述 投票:0回答:1

首先,我用来运行此应用程序的设备运行的是 android 13。

因此,对于这个 gelocator 包,按照包页面中的示例,我已根据我的需要将其添加到此功能中,

void getLocation() async{
    bool serviceEnabled;
    LocationPermission permission;

    // Test if location services are enabled.
    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.');
    }

    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.');
    }
    final LocationSettings locationSettings = LocationSettings(
      accuracy: LocationAccuracy.low,
      distanceFilter: 100,
    );
    Position position = await Geolocator
        .getCurrentPosition(locationSettings: locationSettings);
    print(position);
  }

在此,如果我删除这些部分,

return Future.error('Location services are disabled.');

return Future.error('Location permissions are denied');

如果被拒绝,应用程序每次都会请求许可,如果未打开,操作系统也会要求打开位置服务。

但是,如果我保留它,如上面的代码所示,如果定位权限被拒绝,它只要求一次,并且如果定位服务关闭,操作系统根本不会要求将其打开。然后它只是在控制台中打印异常,而不是在被拒绝时请求许可。

它只请求许可而不是在控制台中打印为未处理的异常更方便,当这些错误返回语句不存在时,我不需要处理该异常。

这不是问题。我只是想知道后台是否发生任何异常处理或者我在这里缺少一些知识吗?

提前致谢!也很抱歉我的英语不好。

flutter geolocator
1个回答
0
投票
void getLocation() async{
    bool serviceEnabled;
    LocationPermission permission;


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

    if (permission == LocationPermission.deniedForever) {
      // One more thing you can do is open App setting
      // TODO(User): Open App Setting 

      return Future.error(
          'Location permissions are permanently denied, we cannot request permissions.');
    }
    final LocationSettings locationSettings = LocationSettings(
      accuracy: LocationAccuracy.low,
      distanceFilter: 100,
    );
    Position position = await Geolocator
        .getCurrentPosition(locationSettings: locationSettings);
    print(position);
  }

请检查重构后的代码,因为根据您的逻辑,isServiceEnable 值将为 false,因此您返回错误并停止执行进一步的程序。

因此,首先,您使用 checkPermission() 方法检查权限,并根据 checkPermission() 函数状态请求位置权限。我假设您已按照文档在清单文件中提供了权限。

© www.soinside.com 2019 - 2024. All rights reserved.