3

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? I don't know, really, it's happening after denied location permissions.

What am I doing wrong and how to fix it?

  • Are you using this plugin ? bcoz exception you listed comes from this library's folder. May be you forgot to add any setup steps for this plugin : https://pub.dev/packages/qr_mobile_vision – Dharmendra Jun 23 '22 at 13:28
  • Yes, I am using this plugin but it was working earlier and it is not even using class when this plugin is imported when this error is happening – Karol Wiśniewski Jun 23 '22 at 13:49
  • Have you tried by updating that library's version ? – Dharmendra Jun 23 '22 at 13:54
  • I am using the newest version. I launch this plugin, I accept permission for this - it's working. Then I go to location permission, denied and still had this error, it's kinda weird – Karol Wiśniewski Jun 23 '22 at 13:58

1 Answers1

3

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 the code.

Below is a code using permission_handler to request permission and Getx to show the contextless dialog. This function will return the status of permission and you can proceed using it by checking if it is allowed or not as

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;
}
Mr Sandhu
  • 101
  • 1
  • 6