使用权限获取当前位置不起作用

Cod*_*eek 5 flutter flutter-dependencies

我正在使用地理定位器:'^3.0.1'permission_handler:'^3.0.0'

现在我想获取用户的当前位置并在用户打开地图时将其显示在地图上。

所以我的代码是:

Future<void> requestPermission() async {
    PermissionHandler()
        .checkPermissionStatus(PermissionGroup.location)
        .then((PermissionStatus permissionStatus) async {
      print("Checking Permission " + permissionStatus.toString());
      if (permissionStatus == PermissionStatus.granted) {
        _getCurrentLocation();
      } else {
        print("Asking Permission " + permissionStatus.toString());

        final List<PermissionGroup> permissions = <PermissionGroup>[
          PermissionGroup.locationWhenInUse
        ];
        final Map<PermissionGroup, PermissionStatus> permissionRequestResult =
            await PermissionHandler().requestPermissions(permissions);

        if (PermissionStatus.granted ==
            permissionRequestResult[PermissionGroup.locationWhenInUse]) {
          print("Permission Granted " + permissionStatus.toString());

          _getCurrentLocation();
        }
      }
    });
  }
Run Code Online (Sandbox Code Playgroud)

和权限在 android 的清单和 IOS 的 info.list 中定义。

现在的问题是当我运行这个函数并调用requestPermission函数时,它会显示要求权限的弹出窗口,

当我的allow the permission应用程序因错误而崩溃时:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions: ... java.lang.IllegalStateException: Reply already submitted

并且权限的结果是Permission.disabled虽然我在应用程序设置中允许了权限,并且在权限中它显示允许位置是权限。但我尝试多次打开应用程序,它显示 Permission.disabled。

即使我deny的应用程序因相同的错误而崩溃。

所以我得出的结论是:

如果我allowdeny 它因为多次请求而崩溃,即使我允许结果是 Permission.disabled。

视频链接: https //youtu.be/A1DKkw6u4HI

谁能帮我解决这个问题?

Or please tell me how to take the current location map easily

Abu*_*lim 5

请告诉我如何轻松获取当前位置图

如果您只需要轻松获取当前位置并且只需要位置权限,则:

您可以使用location带有 flutter 的插件:

在您的pubspec.ymllocation : ^2.3.0

然后获取当前位置:

导入位置包

 import 'package:location/location.dart' as locationPackage;
Run Code Online (Sandbox Code Playgroud)

将此添加到您的州

 locationPackage.Location _locationService = new locationPackage.Location();
     bool _permission = false;
Run Code Online (Sandbox Code Playgroud)

在 initState 中或在需要当前位置时调用此函数

fetchCurrentLocation() async {
    await _locationService.changeSettings(
        accuracy: locationPackage.LocationAccuracy.HIGH, interval: 1000);

    locationPackage.LocationData location;
    // Platform messages may fail, so we use a try/catch PlatformException.
    try {
      bool serviceStatus = await _locationService.serviceEnabled();
      print("Service status: $serviceStatus");
      if (serviceStatus) {
        _permission = await _locationService.requestPermission();
        print("Permission: $_permission");
        if (_permission) {
          location = await _locationService.getLocation();

          print("Location: ${location.latitude}");
        }
      } else {
        bool serviceStatusResult = await _locationService.requestService();
        print("Service status activated after request: $serviceStatusResult");
        if (serviceStatusResult) {
          fetchCurrentLocation();
        }
      }
    } on PlatformException catch (e) {
      print(e);
      if (e.code == 'PERMISSION_DENIED') {
        //error = e.message;
      } else if (e.code == 'SERVICE_STATUS_ERROR') {
        //error = e.message;
      }
      location = null;
    }
  }
Run Code Online (Sandbox Code Playgroud)