Home > other >  How to check permission in Flutter
How to check permission in Flutter

Time:04-29

I am using image_picker and permission_handler. My app gets permission through image_picker. However, when I check the status of the permission_handler, it still shows that the permission is denied. Please help.

Future<void> _pickImage() async{
var status1 = await Permission.photos.status;
var status2 = await Permission.mediaLibrary.status;
var status3 = await Permission.camera.status;
var status4 = await Permission.accessMediaLocation.status;
var status5 = await Permission.storage.status;
var status6 = await Permission.photosAddOnly.status;
print(status1);
print(status2);
print(status3);
print(status4);
print(status5);
print(status6);

if (status1.isDenied) {
  
  ScaffoldMessenger.of(context).showSnackBar(SnackBar(
    content: const Text(
      'permission_denied_message',
      textAlign: TextAlign.center,
      style: TextStyle(
          color: Colors.white
      ),
    ).tr(),
    action: SnackBarAction(
      label: tr('setting'),
      textColor: Colors.yellow[600],
      onPressed: (){
        openAppSettings();
      },
    ),
    backgroundColor: Colors.blue,
    duration: const Duration(seconds: 5),
  ));
}
final pickedImage = await ImagePicker().pickImage(source: ImageSource.gallery);
imageFile = pickedImage != null ? File(pickedImage.path) : null;

_cropImage();

}

CodePudding user response:

Add the following to your "gradle.properties" file:

android.useAndroidX=true
android.enableJetifier=true

Make sure you set the compileSdkVersion in your "android/app/build.gradle" file to 31:

android {
  compileSdkVersion 31
  ...
}

Make sure you replace all the android. dependencies to their AndroidX counterparts. Add permissions to your AndroidManifest.xml file. There's a debug, main and profile versions which are chosen depending on how you start your app. In general, it's sufficient to add permission only to the main version.

  • Related