Reputation: 508
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?
Upvotes: 3
Views: 3390
Reputation: 111
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
}
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;
}
Upvotes: 3