Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A simple Flutter plugin example to show permission handling in Android with Kotlin.
package com.example.my_permission_plugin
import android.Manifest
import android.os.Build
import androidx.core.content.ContextCompat
import io.flutter.plugin.common.MethodCall
import io.flutter.plugin.common.MethodChannel
import io.flutter.plugin.common.MethodChannel.MethodCallHandler
import io.flutter.plugin.common.MethodChannel.Result
import io.flutter.plugin.common.PluginRegistry
import io.flutter.plugin.common.PluginRegistry.Registrar
import java.util.*
import kotlin.collections.ArrayList
class MyPermissionPlugin ( activity: Activity): MethodCallHandler, PluginRegistry.RequestPermissionsResultListener {
val pluginActivity: Activity = activity
private val application: Application = activity.application
private val myPermissionCode = 34264
private var activeResult: Result? = null
private var permissionGranted: Boolean = false
companion object {
fun registerWith(registrar: Registrar) {
val channel = MethodChannel(registrar.messenger(), "my_permission_plugin")
val permissionPlugin = MyPermissionPlugin( registrar.activity())
override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
"initialize" -> initialize( result )
else -> result.notImplemented()
// Note that this method doesn't respond until the permission check is complete
// even if an asynchronous system dialog is required to ask for permission. This
// turns out to be really simple in Flutter since until the result.success|error
// invocation there is no response.
private fun initialize(result: Result) {
if ( null != activeResult ) {
result.error("alreadyInitializing", "Only one initialize call allowed at a time", null )
activeResult = result
checkPermission( application )
// If permission is already granted this completes initialization, otherwise it requests
// a permission check from the system. This is using the READ_EXTERNAL_STORAGE
// permission as an example. Change that to whatever permission(s) your app needs.
private fun checkPermission(context: Application) {
permissionGranted = ContextCompat.checkSelfPermission(context,
Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED
if ( !permissionGranted ) {
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), myPermissionCode )
else {
// Invoked either from the permission result callback or permission check
private fun completeInitialization() {
if ( permissionGranted ) {
// Do some other work if needed
initializedSuccessfully = permissionGranted
// Conveniently plugin invocations are all asynchronous
activeResult = null
// This is invoked after the user chooses an option on the Android permission dialog
// Note that the code to check the grant is simplified, if you request multiple
// permissions you can't make the assumptions this makes.
override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<out String>?,
grantResults: IntArray?): Boolean {
when (requestCode) {
myPermissionCode -> {
if ( null != grantResults ) {
permissionGranted = grantResults.isNotEmpty() &&
grantResults.get(0) == PackageManager.PERMISSION_GRANTED
// Only return true if handling the requestCode
return true
return false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.