title: How to request permissions in a Flutter plugin?
body: <p>I am trying to use a simple Flutter plugin (speech recognition wrapper) and have no idea how to request the appropriate permissions on Android 23 or newer. In the Dart part I have:</p>
  
  <pre><code>  Future requestPermissions() =&gt; 
    _channel.invokeMethod("speech.requestPermissions");
  </code></pre>
  
  <p>In the Android part:</p>
  
  <pre><code>  public class SpeechRecognitionPlugin implements MethodCallHandler, RecognitionListener,
      PluginRegistry.RequestPermissionResultListener {
  </code></pre>
  
  <p>Plugin registration:</p>
  
  <pre><code>  public static void registerWith(Registrar registrar) {
      final MethodChannel channel = new MethodChannel(registrar.messenger(), "speech_recognition");
      SpeechRecognitionPlugin speechRecognitionPlugin = new 
      SpeechRecognitionPlugin(registrar.activity(), channel);
      channel.setMethodCallHandler(speechRecognitionPlugin);
      registrar.addRequestPermissionResultListener(speechRecognitionPlugin);
    }
  </code></pre>
  
  <p>Method call:</p>
  
  <pre><code>else if (call.method.equals("speech.requestPermissions")) {
          Log.d(LOG_TAG, "speech.requestPermissions");
          if (ActivityCompat.shouldShowRequestPermissionRationale(activity,
                  Manifest.permission.RECORD_AUDIO)) {
              Toast.makeText(activity.getApplicationContext(), "This application needs the Record Audio permission for recognition to work", Toast.LENGTH_LONG).show();
          } else {
              Log.d(LOG_TAG, "Requesting permissions");
              ActivityCompat.requestPermissions(activity,
                      new String[]{Manifest.permission.RECORD_AUDIO},
                      1);
          }
          result.success(hasRecordAudioPermission());
  </code></pre>
  
  <p>Result callback:</p>
  
  <pre><code>@Override                                                                                            
  public boolean onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
      boolean granted = false;
      switch (requestCode) {
          case 1: {
              // If request is cancelled, the result arrays are empty.
              if (grantResults.length &gt; 0
                      &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                  granted = true;
              }
              speechChannel.invokeMethod("speech.onPermission", granted);
              return true;
          }
      }
      return false;
  }
  </code></pre>
  
  <p>From logcat I see that the "speech.requestPermissions" call happens, but standard Android system permission request is not shown, just this in the logcat may be related:</p>
  
  <pre><code>D/ViewRootImpl(21171): #1 mView = android.widget.LinearLayout{64f050b 
  V.E...... ......I. 0,0-0,0 #102039d android:id/toast_layout_root}
  D/ViewRootImpl(21171): MSG_RESIZED_REPORT: ci=Rect(0, 0 - 0, 0) vi=Rect(0, 0 - 0, 0) or=1
  D/ViewRootImpl(21171): #3 mView = null
  </code></pre>
  
  <p>What is the correct way to request permissions for Flutter plugins?</p>
  
  <p><strong>EDIT</strong>: This does not apply to the first run, when the dialog shows correctly, but to subsequent runs when the user did not grant the permission at first or revoked it via settings. I realize that changes the question significantly (making it appear as edge case), but Android permissions are not supposed to work that way.</p>
  
  <p><strong>EDIT</strong>: The permissions are present in AndroidManifest.xml</p>