title: How to properly get available cameras
body: <p>So I've been trying a couple of different solutions but none seem to actually work. I am trying to create a selfie function that also has some overlay.</p>
  
  <p>I am using fluro to route to the selfie screen I created where I want to use the camera.</p>
  
  <p>I've tried multiple ways like <code>setState(() {});</code> and the methods below.
  But I don't seem to get any cameras. If I try the demp application that comes with the <code>camera</code> package and it works perfectly fine.</p>
  
  <p>I just don't see how to get the available cameras in a separate component file. There is still a way to get the cameras during the app initialisation and pass the cameras all the way from the app constructor to the component I need it at. But that seems like a bad practice.</p>
  
  <p>Does anyone have a better method.</p>
  
  <p>This is my <code>selfie_component.dart</code> file</p>
  
  <pre class="lang-dart prettyprint-override"><code>import 'package:camera/camera.dart';
  import 'package:flutter/material.dart';
  
  class SelfiePage extends StatefulWidget {
    @override
    State&lt;StatefulWidget&gt; createState() =&gt; new SelfiePageState();
  }
  
  class SelfiePageState extends State&lt;SelfiePage&gt; {
    CameraController _camera;
    CameraLensDirection _direction = CameraLensDirection.back;
  
    @override
    void initState() {
      super.initState();
      _initializeCamera();
    }
  
    Future&lt;CameraDescription&gt; _getCamera(CameraLensDirection dir) async {
      return await availableCameras().then(
        (List&lt;CameraDescription&gt; cameras) =&gt; cameras.firstWhere(
              (CameraDescription camera) =&gt; camera.lensDirection == dir,
            ),
      );
    }
  
    void _initializeCamera() async {
      _camera = CameraController(
        await _getCamera(_direction),
        ResolutionPreset.medium,
      );
      print(_camera);
      await _camera.initialize();
    }
  
    final GlobalKey&lt;ScaffoldState&gt; _scaffoldKey = GlobalKey&lt;ScaffoldState&gt;();
  
    @override
    Widget build(BuildContext context) {
      return new Scaffold(
        key: _scaffoldKey,
        body: Column(
          children: &lt;Widget&gt;[
            Expanded(
              child: Container(
                child: Padding(
                  padding: const EdgeInsets.all(1.0),
                  child: Center(
                    child: _cameraPreviewWidget(),
                  ),
                ),
              ),
            ),
          ],
        ),
      );
    }
  
    /// Display the preview from the camera (or a message if the preview is not available).
    Widget _cameraPreviewWidget() {
      if (_camera == null || !_camera.value.isInitialized) {
        return const Text(
          'Tap a camera',
          style: TextStyle(
            color: Colors.white,
            fontSize: 24.0,
            fontWeight: FontWeight.w900,
          ),
        );
      } else {
        return AspectRatio(
          aspectRatio: _camera.value.aspectRatio,
          child: CameraPreview(_camera),
        );
      }
    }
  }
  </code></pre>