简体   繁体   中英

Flutter unable to request for permissions with permission_handler library

Information

  1. I am testing this on both Android and Linux. The problem appears in both.
  2. I have done the relevant setup according to the page in pub dev. By modifying my grade.properties and android/app/build.gradle

The problem

As the title says the library doesn't work. This is the block of code that I have that requests permissions.

import 'package:permission_handler/permission_handler.dart';

class Perms {
  void requestPermissions() async {
    if (await Permission.storage.request().isGranted) {
      print("Granted.");
    }
  }
}

This is the code sample shown on the page for the library on pub dev. This is the error I get when it hits this block of code. The complete code is linked here

[ERROR:flutter/lib/ui/ui_dart_state.cc(198)] Unhandled Exception: MissingPluginException(No implementation found for method checkPermissionStatus on channel flutter.baseflow.com/permissions/methods)
#0      MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:165:7)
<asynchronous suspension>
#1      MethodChannelPermissionHandler.checkPermissionStatus (package:permission_handler_platform_interface/src/method_channel/method_channel_permission_handler.dart:16:20)
<asynchronous suspension>
#2      Perms.requestPermissions (package:passync/perms.dart:5:18)
<asynchronous suspension>

This is my AndroidManifest.xml file. This is the manifest file located in the main folder. There are other manifest files in debug and profile. I'm not sure if I need to add the same permissions to them too.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.passync">
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>

   <application
        android:label="passync"
        android:name="${applicationName}"
        android:icon="@mipmap/ic_launcher">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:launchMode="singleTop"
            android:theme="@style/LaunchTheme"
            android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
            android:hardwareAccelerated="true"
            android:windowSoftInputMode="adjustResize">
            <!-- Specifies an Android theme to apply to this Activity as soon as
                 the Android process has started. This theme is visible to the user
                 while the Flutter UI initializes. After that, this theme continues
                 to determine the Window background behind the Flutter UI. -->
            <meta-data
              android:name="io.flutter.embedding.android.NormalTheme"
              android:resource="@style/NormalTheme"
              />
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <!-- Don't delete the meta-data below.
             This is used by the Flutter tool to generate GeneratedPluginRegistrant.java -->
        <meta-data
            android:name="flutterEmbedding"
            android:value="2" />
    </application>
</manifest>

What I have tried

  1. Create a new project and duplicate the code. Ended up with the same error.
  2. Ran flutter clean as suggested in many SO answers. Still failed.
  3. Request other permissions to see if it's a storage-specific thing. I get the same error even when asking for different permissions.

Tell me if there's any missing information, and I'll edit the post. Thanks.

As the time of this writing permission_handler plugin is only available for Android , iOS , and Windows . It's going to fail with MissingPluginException on every other platform.

Also, permission_handler has a set-up to be followed for both Android and iOS . Please read the instructions from the official docs . Below is a copy-paste from the docs:

Android

AndroidX

As of version 3.1.0 the permission_handler plugin switched to the AndroidX version of the Android Support Libraries. This means you need to make sure your Android project is also upgraded to support AndroidX. Detailed instructions can be found here .

The TL;DR version is:

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

     android.useAndroidX=true android.enableJetifier=true
  2. Make sure you set the compileSdkVersion in your android/app/build.gradle file to 33 :

     android { compileSdkVersion 33 ... }
  3. Make sure you replace all the Android dependencies to their AndroidX counterparts (a full list can be found here ).

Add permissions to your AndroidManifest.xml file. There's a debug , main , and profile version which are chosen depending on how you start your app. In general, it's sufficient to add permission only to the main version. Here is an example AndroidManifest.xml with a complete list of all possible permissions.

iOS

Add permission to your Info.plist file. Here is an example Info.plist with a complete list of all possible permissions.

IMPORTANT: This is because the permission_handler plugin touches all different SDKs and because the static code analyser (run by Apple upon App submission) detects this and will assert if it cannot find a matching permission option in the Info.plist . More information about this can be found here .

The permission_handler plugin uses macros to control whether permission is enabled.

You must list permission you want to use in your application:

  1. Add the following to your Podfile file:

     post_install do |installer| installer.pods_project.targets.each do |target| ... # Here are some configurations automatically generated by flutter # Start of the permission_handler configuration target.build_configurations.each do |config| # You can enable the permissions needed here. For example to enable camera # permission, just remove the `#` character in front so it looks like this: # # ## dart: PermissionGroup.camera # 'PERMISSION_CAMERA=1' # # Preprocessor definitions can be found in: https://github.com/Baseflow/flutter-permission-handler/blob/master/permission_handler_apple/ios/Classes/PermissionHandlerEnums.h config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [ '$(inherited)', ## dart: PermissionGroup.calendar # 'PERMISSION_EVENTS=1', ## dart: PermissionGroup.reminders # 'PERMISSION_REMINDERS=1', ## dart: PermissionGroup.contacts # 'PERMISSION_CONTACTS=1', ## dart: PermissionGroup.camera # 'PERMISSION_CAMERA=1', ## dart: PermissionGroup.microphone # 'PERMISSION_MICROPHONE=1', ## dart: PermissionGroup.speech # 'PERMISSION_SPEECH_RECOGNIZER=1', ## dart: PermissionGroup.photos # 'PERMISSION_PHOTOS=1', ## dart: [PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse] # 'PERMISSION_LOCATION=1', ## dart: PermissionGroup.notification # 'PERMISSION_NOTIFICATIONS=1', ## dart: PermissionGroup.mediaLibrary # 'PERMISSION_MEDIA_LIBRARY=1', ## dart: PermissionGroup.sensors # 'PERMISSION_SENSORS=1', ## dart: PermissionGroup.bluetooth # 'PERMISSION_BLUETOOTH=1', ## dart: PermissionGroup.appTrackingTransparency # 'PERMISSION_APP_TRACKING_TRANSPARENCY=1', ## dart: PermissionGroup.criticalAlerts # 'PERMISSION_CRITICAL_ALERTS=1' ] end # End of the permission_handler configuration end end
  2. Remove the # character in front of the permission you do want to use. For example, if you need access to the calendar make sure the code looks like this:

     ## dart: PermissionGroup.calendar 'PERMISSION_EVENTS=1',
  3. Delete the corresponding permission description in Info.plist eg when you don't need camera permission, just delete 'NSCameraUsageDescription' The following lists the relationship between Permission and The key of Info.plist :

    Permission Info.plist Macro
    PermissionGroup.calendar NSCalendarsUsageDescription PERMISSION_EVENTS
    PermissionGroup.reminders NSRemindersUsageDescription PERMISSION_REMINDERS
    PermissionGroup.contacts NSContactsUsageDescription PERMISSION_CONTACTS
    PermissionGroup.camera NSCameraUsageDescription PERMISSION_CAMERA
    PermissionGroup.microphone NSMicrophoneUsageDescription PERMISSION_MICROPHONE
    PermissionGroup.speech NSSpeechRecognitionUsageDescription PERMISSION_SPEECH_RECOGNIZER
    PermissionGroup.photos NSPhotoLibraryUsageDescription PERMISSION_PHOTOS
    PermissionGroup.location, PermissionGroup.locationAlways, PermissionGroup.locationWhenInUse NSLocationUsageDescription, NSLocationAlwaysAndWhenInUseUsageDescription, NSLocationWhenInUseUsageDescription PERMISSION_LOCATION
    PermissionGroup.notification PermissionGroupNotification PERMISSION_NOTIFICATIONS
    PermissionGroup.mediaLibrary NSAppleMusicUsageDescription, kTCCServiceMediaLibrary PERMISSION_MEDIA_LIBRARY
    PermissionGroup.sensors NSMotionUsageDescription PERMISSION_SENSORS
    PermissionGroup.bluetooth NSBluetoothAlwaysUsageDescription, NSBluetoothPeripheralUsageDescription PERMISSION_BLUETOOTH
    PermissionGroup.appTrackingTransparency NSUserTrackingUsageDescription PERMISSION_APP_TRACKING_TRANSPARENCY
    PermissionGroup.criticalAlerts PermissionGroupCriticalAlerts PERMISSION_CRITICAL_ALERTS
  4. Clean & Rebuild

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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