title: Granting permission using camera package pauses the debugger on an exception
body: <p>I am using the <a href="https://pub.dev/packages/camera" rel="nofollow noreferrer">camera</a> package for a simple functionality. I am mostly following the <a href="https://github.com/flutter/plugins/blob/master/packages/camera/example/lib/main.dart" rel="nofollow noreferrer">example</a> the package provided. When I open the camera widget page, the package automatically prompts to provide permission to the camera and microphone. After clicking allow to both the permissions the debugger is paused with an exception:</p>
  
  <pre><code>Exception has occurred.
  FlutterError (A CameraController was used after being disposed.
  Once you have called dispose() on a CameraController, it can no longer be used.).
  </code></pre>
  
  <p>Here is the required code :</p>
  
  <pre><code>class CameraPage extends StatefulWidget {
    @override
    _CameraPageState createState() =&gt; _CameraPageState();
  }
  
  class _CameraPageState extends State&lt;CameraPage&gt;
      with WidgetsBindingObserver {
    CameraController _controller;
    List&lt;CameraDescription&gt; _availableCameras;
    ...
  
    @override
    void initState() {
      super.initState();
      WidgetsBinding.instance.addObserver(this);
      _initialize();
    }
  
    Future&lt;void&gt; _initialize() async {
      await _getCameras();
      _controller = CameraController(_availableCameras[0], ResolutionPreset.high);
      await _controller.initialize();
      if (!mounted) {
        return;
      }
      setState(() {});
    }
  
    @override
    void didChangeAppLifecycleState(AppLifecycleState state) {
      if (state == AppLifecycleState.inactive) {
        _controller?.dispose();
      } else if (state == AppLifecycleState.resumed) {
        if (_controller != null) {
          _setCurrentCamera(_controller.description);
        }
      }
    }
  
    @override
    void dispose() {
      WidgetsBinding.instance.removeObserver(this);
      _controller.dispose();
      super.dispose();
    }
  
  
    Future&lt;List&lt;CameraDescription&gt;&gt; _getCameras() async {
      List&lt;CameraDescription&gt; camDescriptions;
        camDescriptions = await availableCameras();
  
        _availableCameras = camDescriptions;
      return camDescriptions;
    }
  
    @override
    Widget build(BuildContext context) {
      ...
    }
  
  
    Future&lt;void&gt; _setCurrentCamera(CameraDescription cameraDescription) async {
      if (_controller != null) {
        await _controller.dispose();
      }
      _controller = CameraController(
        cameraDescription,
        ResolutionPreset.high,
        enableAudio: false,
      );
  
      // If the _controller is updated then update the UI.
      _controller.addListener(() {
        if (mounted) setState(() {});
  
        if (_controller.value.hasError) {
          print('Camera error ${_controller.value.errorDescription}');
        }
      });
  
      try {
        await _controller.initialize();
      } on CameraException catch (e) {
        _showCameraException(e);
      }
  
      if (mounted) {
        setState(() {});
      }
    }
  
    void _switchCamera() {
      if (_controller != null &amp;&amp; !_controller.value.isRecordingVideo) {
        CameraLensDirection direction = _controller.description.lensDirection;
        CameraLensDirection required = direction == CameraLensDirection.front
            ? CameraLensDirection.back
            : CameraLensDirection.front;
        for (CameraDescription cameraDescription in _availableCameras) {
          if (cameraDescription.lensDirection == required) {
            _setCurrentCamera(cameraDescription);
            return;
          }
        }
      }
    }
  
  
    void _showCameraException(CameraException e) {
      String errorText = 'Error: ${e.code}\nError Message: ${e.description}';
      print(errorText);
    }
  
  }
  </code></pre>
  
  <p>The debugger is pointing the exception here:</p>
  
  <pre><code>  Future&lt;void&gt; _initialize() async {
      await _getCameras();
      _controller = CameraController(_availableCameras[0], ResolutionPreset.high);
      //-------------HERE------------------
      await _controller.initialize();
      if (!mounted) {
        return;
      }
      setState(() {});
    }
  </code></pre>
  
  <p>Once I resume the debugger and try opening this camera page again there is no error/exception anymore. It Is happening only after accepting permissions for the first time.</p>