简体   繁体   English

在flutter中检查位置权限是否启用

[英]check location permission is enable or not in flutter

I am using below code to check the status of location permission when user navigate back from setting screen to check the location permission is enable or now.当用户从设置屏幕返回以检查位置权限是否启用或现在时,我使用下面的代码来检查位置权限的状态。

checkPermission() async {
    bool isLocationEnabled = await Geolocator().isLocationServiceEnabled();
    if (isLocationEnabled) {
      print("enable");
    } else {
      print("not enable");
    }
  }

navigate user to setting screen by this code : PermissionHandler().openAppSettings();通过以下代码将用户导航到设置屏幕: PermissionHandler().openAppSettings();

But the problem is locationserviceenable is always says true whether the location permission is disable or enable by user.但问题是 locationserviceenable 总是说 true 无论位置权限是禁用还是由用户启用。

you should use this method (you can see the doc here ):您应该使用此方法(您可以在此处查看文档):

bool isLocationEnabled = await _locationService.serviceEnabled();

the flutter document says:颤振文件说:

serviceEnabled(): Return a boolean to know if the Location Service is enabled or if the user manually deactivated it. serviceEnabled():返回一个布尔值以了解位置服务是否已启用或用户是否手动停用它。

for more information you can take a look at this example .有关更多信息,您可以查看此示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM