简体   繁体   English

Flutter 应用程序在华为位置拒绝位置权限时崩溃

[英]Flutter app crashing when permission for location is denied with Huawei Location

I am implementing getting device location for Huawei devices, it is working when permission is granted but when is denied app is crashing.我正在为华为设备实现获取设备位置,当授予权限但被拒绝时应用程序崩溃时它正在工作。

With location from google it never happened.从谷歌的位置它从来没有发生过。

Here is my code for getting location:这是我获取位置的代码:

Future<Location?> getAccuratePositionH() async {
  PermissionHandler permissionHandler = PermissionHandler();
  bool status = await permissionHandler.requestLocationPermission();
  if (status) {
    FusedLocationProviderClient locationService = FusedLocationProviderClient();
    Location location = await locationService.getLastLocation();
    return location;
  }
  else {
    return null;
  }
}

This is what I am getting in console:这就是我在控制台中得到的:

I/cgr.qrmv.QrMobVisPlugin( 5178): Permissions request denied.
W/cgr.qrmv.QrMobVisPlugin( 5178): Starting QR Mobile Vision failed
W/cgr.qrmv.QrMobVisPlugin( 5178): com.github.rmtmckenzie.qrmobilevision.QrReader$Exception: QR reader failed because noPermissions

and

java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=1, result=-1, data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has extras) }} to activity {com.lea24.partyfinder/com.lea24.partyfinder.MainActivity}: java.lang.NullPointerException: Attempt to read from field 'io.flutter.plugin.common.MethodChannel$Result com.github.rmtmckenzie.qrmobilevision.QrMobileVisionPlugin$ReadingInstance.startResult' on a null object reference

Why is here QR Mobile Vision?为什么这里有 QR 移动视觉? I don't know, really, it's happening after denied location permissions.我不知道,真的,这是在拒绝位置权限后发生的。

What am I doing wrong and how to fix it?我做错了什么以及如何解决?

I'm late but it might help others who are facing this issue, I was facing the same issue and after a lot of struggle I figured out the reason and solution that was working for me.我迟到了,但它可能会帮助其他面临这个问题的人,我面临着同样的问题,经过一番努力,我找到了对我有用的原因和解决方案。 Hope it will work for you too.希望它也对你有用。


If permission is denied once it is denied permanently.如果权限一旦被永久拒绝就被拒绝。 So, users have to change it from settings manually.因此,用户必须手动从设置中更改它。 All you can do is redirect the user to settings.您所能做的就是将用户重定向到设置。 Before asking for permission make sure permission is not already denied otherwise it will crash your app without any warning, as given below in code.在请求许可之前,请确保尚未拒绝许可,否则它将在没有任何警告的情况下使您的应用程序崩溃,如下面的代码所示。

Below is a code which is using permission_handler to request permission and Getx to show contextless dialog.下面是使用 permission_handler 请求权限和 Getx 显示无上下文对话框的代码。 This function will return status of permission and you can proceed using it by checking if it is allowed or not as此 function 将返回权限状态,您可以通过检查是否允许继续使用它

PermissionStatus status = await requestLocalStoragePermission();
if (status.isGranted) {
//proceed
}

Full Code完整代码


import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:permission_handler/permission_handler.dart';

Future<PermissionStatus> requestLocalStoragePermission() async {

  PermissionStatus status;
  if ((await Permission.storage.isPermanentlyDenied) ||
      (await Permission.storage.isDenied)) {
    status = PermissionStatus.denied;
    Get.dialog(
      AlertDialog(
        //Getx dialog is used, you may use default other dialog based on your requirement
        title: const Text(
          "Storage permission required!",
          textAlign: TextAlign.center,
          style: TextStyle(
            fontWeight: FontWeight.bold,
          ),
        ),
        shape:
            RoundedRectangleBorder(borderRadius: BorderRadius.circular(30.0)),
        content: const Text(
          "Storage permission is required to download files",
          textAlign: TextAlign.center,
        ),
        actions: <Widget>[
          Center(
            child: TextButton(
              onPressed: () async => {
                await openAppSettings(), //function in permission_handler
                Get.back() //close dialog
              },
              child: const Text("Grant Permission"),
            ),
          ),
        ],
      ),
    );
  } else {
    try {
      status = await Permission.storage.request();
    } catch (err) {
      status = PermissionStatus.denied;
    }
  }
  return status;
}

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

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