title: Kotlin Type mismatch: inferred type is Activity? but Context was expected ERROR after upgrading flutter to the 3.0.0
body: <p>I am using <a href="https://github.com/ravinandan25/simple_beacons_flutter/tree/master/android" rel="noreferrer">beacon plugin</a>. After I upgraded flutter to the version 3.0.0, it throwed this error:</p>
  <pre><code>Class 'BeaconsPlugin' is not abstract and does not implement abstract member public abstract fun onRequestPermissionsResult(p0: Int, p1: Array&lt;(out) String!&gt;, p2: IntArray): Boolean defined in io.flutter.plugin.common.PluginRegistry.RequestPermissionsResultListener
  e: C:\Software\HR-Management-Localization\mapping_tool\packages\simple_beacons_flutter\android\src\main\kotlin\com\umair\beacons_plugin\BeaconsPlugin.kt: (66, 32): Type mismatch: inferred type is Activity? but Context was expected
  e: C:\Software\HR-Management-Localization\mapping_tool\packages\simple_beacons_flutter\android\src\main\kotlin\com\umair\beacons_plugin\BeaconsPlugin.kt: (428, 5): 'onRequestPermissionsResult' overrides nothing
  </code></pre>
  <p>I've solved it by removing question marks from lines</p>
  <pre><code>permissions: Array&lt;out String&gt;?,
          grantResults: IntArray?
  </code></pre>
  <p>to this</p>
  <pre><code>permissions: Array&lt;out String&gt;,
          grantResults: IntArray
  </code></pre>
  <p>in <code>onRequestPermissionsResult</code> method.</p>
  <p>And than it throwed a different error: <code>BeaconsPlugin.kt: (66, 32): Type mismatch: inferred type is Activity? but Context was expected</code></p>
  <p>I don't know kotlin therefore unable to solve this.</p>
  <p>Here is my whole <code>BeaconsPlugin.kt</code>:</p>
  <pre><code>package com.umair.beacons_plugin
  
  import android.Manifest
  import android.app.Activity
  import android.app.AlertDialog
  import android.content.Context
  import android.content.Intent
  import android.content.pm.PackageManager
  import android.os.Build
  import androidx.annotation.NonNull
  import androidx.core.app.ActivityCompat
  import androidx.core.content.ContextCompat
  import io.flutter.embedding.engine.plugins.FlutterPlugin
  import io.flutter.embedding.engine.plugins.activity.ActivityAware
  import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding
  import io.flutter.plugin.common.*
  import timber.log.Timber
  
  
  /** BeaconsPlugin */
  class BeaconsPlugin : FlutterPlugin, ActivityAware,
      PluginRegistry.RequestPermissionsResultListener {
  
      private var context: Context? = null
  
      override fun onAttachedToEngine(@NonNull flutterPluginBinding: FlutterPlugin.FlutterPluginBinding) {
          Timber.i(&quot;onAttachedToEngine&quot;)
          messenger = flutterPluginBinding.binaryMessenger
          setUpPluginMethods(
              flutterPluginBinding.applicationContext,
              flutterPluginBinding.binaryMessenger
          )
          context = flutterPluginBinding.applicationContext
          beaconHelper = BeaconHelper(flutterPluginBinding.applicationContext)
          context?.let {
              BeaconPreferences.init(it)
              stopBackgroundService(it)
          }
      }
  
      companion object {
          private val TAG = &quot;BeaconsPlugin&quot;
          private var REQUEST_LOCATION_PERMISSIONS = 1890
          private var PERMISSION_REQUEST_BACKGROUND_LOCATION = 1891
          private var channel: MethodChannel? = null
          private var event_channel: EventChannel? = null
          private var currentActivity: Activity? = null
          private var beaconHelper: BeaconHelper? = null
  
          private var defaultPermissionDialogTitle = &quot;This app needs background location access&quot;
          private var defaultPermissionDialogMessage =
              &quot;Please grant location access so this app can detect beacons in the background.&quot;
  
          @JvmStatic
          internal var messenger: BinaryMessenger? = null
  
          @JvmStatic
          fun registerWith(registrar: PluginRegistry.Registrar) {
              BeaconPreferences.init(registrar.context())
              if (beaconHelper == null) {
                  this.beaconHelper = BeaconHelper(registrar.context())
              }
              val instance = BeaconsPlugin()
              registrar.addRequestPermissionsResultListener(instance)
              //requestPermission()
              setUpPluginMethods(registrar.activity(), registrar.messenger())
          }
  
          @JvmStatic
          fun registerWith(messenger: BinaryMessenger, context: Context) {
              BeaconPreferences.init(context)
              if (beaconHelper == null) {
                  this.beaconHelper = BeaconHelper(context)
              }
              val instance = BeaconsPlugin()
              //requestPermission()
              setUpPluginMethods(context, messenger)
          }
  
          @JvmStatic
          fun registerWith(messenger: BinaryMessenger, beaconHelper: BeaconHelper, context: Context) {
              BeaconPreferences.init(context)
              this.beaconHelper = beaconHelper
              val instance = BeaconsPlugin()
              //requestPermission()
              setUpPluginMethods(context, messenger)
          }
  
          @JvmStatic
          private fun setUpPluginMethods(context: Context, messenger: BinaryMessenger) {
              Timber.plant(Timber.DebugTree())
  
              channel = MethodChannel(messenger, &quot;beacons_plugin&quot;)
              notifyIfPermissionsGranted(context)
              channel?.setMethodCallHandler { call, result -&gt;
                  when {
                      call.method == &quot;startMonitoring&quot; -&gt; {
                          stopService = false
                          callBack?.startScanning()
                          result.success(&quot;Started scanning Beacons.&quot;)
                      }
                      call.method == &quot;stopMonitoring&quot; -&gt; {
  
                          if (runInBackground) {
                              stopService = true
                              context.let {
                                  stopBackgroundService(it)
                              }
                          }
  
                          callBack?.stopMonitoringBeacons()
                          result.success(&quot;Stopped scanning Beacons.&quot;)
                      }
                      call.method == &quot;addRegion&quot; -&gt; {
                          callBack?.addRegion(call, result)
                      }
                      call.method == &quot;clearRegions&quot; -&gt; {
                          callBack?.clearRegions(call, result)
                      }
                      call.method == &quot;runInBackground&quot; -&gt; {
                          call.argument&lt;Boolean&gt;(&quot;background&quot;)?.let {
                              runInBackground = it
                          }
                          result.success(&quot;App will run in background? $runInBackground&quot;)
                      }
                      call.method == &quot;clearDisclosureDialogShowFlag&quot; -&gt; {
                          call.argument&lt;Boolean&gt;(&quot;clearFlag&quot;)?.let {
                              if (it) {
                                  clearPermissionDialogShownFlag()
                                  result.success(&quot;clearDisclosureDialogShowFlag: Flag cleared!&quot;)
                              } else {
                                  setPermissionDialogShown()
                                  result.success(&quot;clearDisclosureDialogShowFlag: Flag Set!&quot;)
                              }
                          }
                      }
                      call.method == &quot;setDisclosureDialogMessage&quot; -&gt; {
                          call.argument&lt;String&gt;(&quot;title&quot;)?.let {
                              defaultPermissionDialogTitle = it
                          }
                          call.argument&lt;String&gt;(&quot;message&quot;)?.let {
                              defaultPermissionDialogMessage = it
                          }
                          requestPermission()
                          result.success(&quot;Disclosure message Set: $defaultPermissionDialogMessage&quot;)
                      }
                      call.method == &quot;addBeaconLayoutForAndroid&quot; -&gt; {
                          call.argument&lt;String&gt;(&quot;layout&quot;)?.let {
                              callBack?.addBeaconLayout(it)
                              result.success(&quot;Beacon layout added: $it&quot;)
                          }
                      }
                      call.method == &quot;setForegroundScanPeriodForAndroid&quot; -&gt; {
                          var foregroundScanPeriod = 1100L
                          var foregroundBetweenScanPeriod = 0L
                          call.argument&lt;Int&gt;(&quot;foregroundScanPeriod&quot;)?.let {
                              if (it &gt; foregroundScanPeriod) {
                                  foregroundScanPeriod = it.toLong()
                              }
                          }
                          call.argument&lt;Int&gt;(&quot;foregroundBetweenScanPeriod&quot;)?.let {
                              if (it &gt; foregroundBetweenScanPeriod) {
                                  foregroundBetweenScanPeriod = it.toLong()
                              }
                          }
                          callBack?.setForegroundScanPeriod(
                              foregroundScanPeriod = foregroundScanPeriod,
                              foregroundBetweenScanPeriod = foregroundBetweenScanPeriod
                          )
                          result.success(&quot;setForegroundScanPeriod updated.&quot;)
                      }
                      call.method == &quot;setBackgroundScanPeriodForAndroid&quot; -&gt; {
                          var backgroundScanPeriod = 1100L
                          var backgroundBetweenScanPeriod = 0L
                          call.argument&lt;Int&gt;(&quot;backgroundScanPeriod&quot;)?.let {
                              if (it &gt; backgroundScanPeriod) {
                                  backgroundScanPeriod = it.toLong()
                              }
                          }
                          call.argument&lt;Int&gt;(&quot;backgroundBetweenScanPeriod&quot;)?.let {
                              if (it &gt; backgroundBetweenScanPeriod) {
                                  backgroundBetweenScanPeriod = it.toLong()
                              }
                          }
                          callBack?.setBackgroundScanPeriod(
                              backgroundScanPeriod = backgroundScanPeriod,
                              backgroundBetweenScanPeriod = backgroundBetweenScanPeriod
                          )
                          result.success(&quot;setBackgroundScanPeriod updated.&quot;)
                      }
                      else -&gt; result.notImplemented()
                  }
              }
  
              event_channel = EventChannel(messenger, &quot;beacons_plugin_stream&quot;)
              event_channel?.setStreamHandler(object : EventChannel.StreamHandler {
                  override fun onListen(arguments: Any?, events: EventChannel.EventSink?) {
                      callBack?.setEventSink(events)
                  }
  
                  override fun onCancel(arguments: Any?) {
  
                  }
              })
          }
  
          @JvmStatic
          private fun notifyIfPermissionsGranted(context: Context) {
              if (permissionsGranted(context)) {
                  doIfPermissionsGranted()
              }
          }
  
          @JvmStatic
          fun permissionsGranted(context: Context): Boolean {
              return ContextCompat.checkSelfPermission(
                  context,
                  Manifest.permission.ACCESS_FINE_LOCATION
              ) == PackageManager.PERMISSION_GRANTED &amp;&amp;
                      ContextCompat.checkSelfPermission(
                          context,
                          Manifest.permission.ACCESS_COARSE_LOCATION
                      ) == PackageManager.PERMISSION_GRANTED
          }
  
          @JvmStatic
          private fun doIfPermissionsGranted() {
              Timber.i(&quot;doIfPermissionsGranted&quot;)
  
              if (beaconHelper == null) {
                  return
              }
  
              this.callBack = beaconHelper
  
              sendBLEScannerReadyCallback()
          }
  
  
          @JvmStatic
          private fun requestPermission() {
              if (areBackgroundScanPermissionsGranted()) {
                  requestLocationPermissions()
              } else {
                  requestBackgroundPermission()
              }
          }
  
          private fun requestLocationPermissions() {
              if (!arePermissionsGranted()) {
                  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.Q) {
                          currentActivity?.let {
                              ActivityCompat.requestPermissions(
                                  it,
                                  arrayOf(
                                      Manifest.permission.ACCESS_FINE_LOCATION,
                                      Manifest.permission.ACCESS_COARSE_LOCATION,
                                      Manifest.permission.ACCESS_BACKGROUND_LOCATION
                                  ),
                                  REQUEST_LOCATION_PERMISSIONS
                              )
                          }
                      } else {
                          currentActivity?.let {
                              ActivityCompat.requestPermissions(
                                  it,
                                  arrayOf(
                                      Manifest.permission.ACCESS_FINE_LOCATION,
                                      Manifest.permission.ACCESS_COARSE_LOCATION
                                  ),
                                  REQUEST_LOCATION_PERMISSIONS
                              )
                          }
                      }
                  } else {
                      doIfPermissionsGranted()
                  }
              } else {
                  doIfPermissionsGranted()
              }
          }
  
          @JvmStatic
          private fun requestBackgroundPermission() {
              if (!isPermissionDialogShown()) {
                  currentActivity?.let {
                      if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.Q) {
                          //if (it.shouldShowRequestPermissionRationale(Manifest.permission.ACCESS_BACKGROUND_LOCATION)) {
                          val builder: AlertDialog.Builder =
                              AlertDialog.Builder(it)
                          builder.setTitle(defaultPermissionDialogTitle)
                          builder.setMessage(defaultPermissionDialogMessage)
                          builder.setPositiveButton(&quot;Ok&quot;, null)
                          builder.setOnDismissListener {
                              setPermissionDialogShown()
                              requestLocationPermissions()
                              channel?.invokeMethod(&quot;isPermissionDialogShown&quot;, &quot;true&quot;)
                          }
                          builder.show()
                          //}
                      }
                  }
              }
          }
  
          @JvmStatic
          private fun arePermissionsGranted(): Boolean {
              currentActivity?.let {
                  return ContextCompat.checkSelfPermission(
                      it,
                      Manifest.permission.ACCESS_FINE_LOCATION
                  ) == PackageManager.PERMISSION_GRANTED &amp;&amp;
                          ContextCompat.checkSelfPermission(
                              it,
                              Manifest.permission.ACCESS_COARSE_LOCATION
                          ) == PackageManager.PERMISSION_GRANTED
              }
              return false
          }
  
          @JvmStatic
          private fun areBackgroundScanPermissionsGranted(): Boolean {
              currentActivity?.let {
                  return if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.Q) {
                      ContextCompat.checkSelfPermission(
                          it,
                          Manifest.permission.ACCESS_BACKGROUND_LOCATION
                      ) == PackageManager.PERMISSION_GRANTED
                  } else {
                      return true
                  }
              }
              return true
          }
  
          @JvmStatic
          var runInBackground = false
  
          @JvmStatic
          var stopService = false
  
          interface PluginImpl {
              fun startScanning()
              fun stopMonitoringBeacons()
              fun addRegion(call: MethodCall, result: MethodChannel.Result)
              fun clearRegions(call: MethodCall, result: MethodChannel.Result)
              fun setEventSink(events: EventChannel.EventSink?)
              fun addBeaconLayout(layout: String)
              fun setForegroundScanPeriod(
                  foregroundScanPeriod: Long,
                  foregroundBetweenScanPeriod: Long
              )
  
              fun setBackgroundScanPeriod(
                  backgroundScanPeriod: Long,
                  backgroundBetweenScanPeriod: Long
              )
          }
  
          private var callBack: PluginImpl? = null
  
          fun sendBLEScannerReadyCallback() {
              channel?.invokeMethod(&quot;scannerReady&quot;, &quot;&quot;)
          }
  
          fun startBackgroundService(context: Context) {
              if (runInBackground &amp;&amp; !stopService) {
                  val serviceIntent1 = Intent(context, BeaconsDiscoveryService::class.java)
                  context.startService(serviceIntent1)
              }
          }
  
          fun stopBackgroundService(context: Context) {
              if (runInBackground &amp;&amp; !stopService) {
                  val serviceIntent = Intent(context, BeaconsDiscoveryService::class.java)
                  context.stopService(serviceIntent)
              }
          }
      }
  
      override fun onDetachedFromEngine(@NonNull binding: FlutterPlugin.FlutterPluginBinding) {
          currentActivity = null
          channel?.setMethodCallHandler(null)
          event_channel?.setStreamHandler(null)
  
          if (!BeaconsPlugin.runInBackground)
              beaconHelper?.stopMonitoringBeacons()
  
          context?.let {
              stopBackgroundService(it)
          }
  
          context = null
      }
  
      override fun onAttachedToActivity(activityPluginBinding: ActivityPluginBinding) {
          currentActivity = activityPluginBinding.activity
          BeaconPreferences.init(currentActivity)
          activityPluginBinding.addRequestPermissionsResultListener(this)
          //requestPermission()
  
          if (arePermissionsGranted()) {
              sendBLEScannerReadyCallback()
          }
      }
  
      override fun onDetachedFromActivityForConfigChanges() {
          Timber.i(&quot;onDetachedFromActivityForConfigChanges&quot;)
      }
  
      override fun onReattachedToActivityForConfigChanges(activityPluginBinding: ActivityPluginBinding) {
          Timber.i(&quot;onReattachedToActivityForConfigChanges&quot;)
          currentActivity = activityPluginBinding.activity
          activityPluginBinding.addRequestPermissionsResultListener(this)
      }
  
      override fun onDetachedFromActivity() {
          Timber.i(&quot;onDetachedFromActivity&quot;)
          currentActivity = null
  
          context?.let {
              startBackgroundService(it)
          }
      }
  
  
      override fun onRequestPermissionsResult(
          requestCode: Int,
          permissions: Array&lt;out String&gt;,
          grantResults: IntArray
      ): Boolean {
          if (requestCode == REQUEST_LOCATION_PERMISSIONS &amp;&amp; grantResults?.isNotEmpty()!! &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              doIfPermissionsGranted()
              return true
          }
          if (requestCode == PERMISSION_REQUEST_BACKGROUND_LOCATION &amp;&amp; grantResults?.isNotEmpty()!! &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
              //setPermissionDialogShown()
              //requestPermission()
              return true
          }
          return false
      }
  }
  </code></pre>