Flutter 正在获取具有权限的当前位置不起作用

Flutter 正在获取具有权限的当前位置不起作用,flutter,flutter-dependencies,Flutter,Flutter Dependencies,我正在使用 地理定位器:“^3.0.1” 权限处理程序:“^3.0.0” 现在,我想获取用户的当前位置,并在用户打开地图时将其显示在地图上 所以我的代码是: Future<void> requestPermission() async { PermissionHandler() .checkPermissionStatus(PermissionGroup.location) .then((PermissionStatus permissionS

我正在使用 地理定位器:“^3.0.1” 权限处理程序:“^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();
        }
      }
    });
  }
Future requestPermission()异步{
PermissionHandler()
.checkPermissionStatus(PermissionGroup.location)
.然后((PermissionStatus PermissionStatus)异步{
打印(“检查权限”+permissionStatus.toString());
if(permissionStatus==permissionStatus.grated){
_getCurrentLocation();
}否则{
打印(“请求许可”+permissionStatus.toString());
最终列表权限=[
许可组。使用时的位置
];
最终地图许可请求结果=
等待PermissionHandler().requestPermissions(权限);
如果(PermissionStatus.grated==
permissionRequestResult[PermissionGroup.locationWhenInUse]){
打印(“已授予权限”+permissionStatus.toString());
_getCurrentLocation();
}
}
});
}
和权限在android的清单和IOS的info.list中定义

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

当我
允许权限时
应用程序因错误而崩溃:

java.lang.RuntimeException:未能传递结果结果信息{who=@android:requestPermissions:…java.lang.IllegalStateException:答复已提交

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

即使我拒绝,应用程序也会以同样的错误崩溃

因此,我的结论是:

如果我
allow
deny
它会崩溃,因为它多次请求,即使我允许,结果也是Permission.disabled

视频链接:

有人能帮我解决这个问题吗

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

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

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

您可以将
location
插件与flatter一起使用:

在您的
publispec.yml
位置:^2.3.0

然后,要获取当前位置,请执行以下操作:

导入位置包

 import 'package:location/location.dart' as locationPackage;
 locationPackage.Location _locationService = new locationPackage.Location();
     bool _permission = false;
将此添加到您所在的州

 import 'package:location/location.dart' as locationPackage;
 locationPackage.Location _locationService = new locationPackage.Location();
     bool _permission = false;
在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;
    }
  }

我会检查这个oode并回复给你。@CodeGeek你检查过了吗?行吗?