title: How to wait for camera to initialize?
body: <p>I kept getting an error from the camera.dart that &quot;name&quot; was being called on a null object.</p>
  <p>After some time, I realized that the problem was the that the build method is called before the async code in my initstate finished (I'm actually slightly proud that I understood the problem at least :))</p>
  <p>I tried many different ways to initialize my camera properly, but I could not.
  This is the last iteration of my code.</p>
  <p>What's the idiomatic way of handling this future?</p>
  <pre><code>  class _PicturePreviewState extends State&lt;PicturePreview&gt; {
    List&lt;CameraDescription&gt; cameras;
    CameraDescription camera;
    CameraController cameraController;
    Future&lt;void&gt; initializeController;
  
    Future&lt;void&gt; getCameras() async {
      try {
        cameras = await availableCameras();     
      } catch(e) {print(e);}
      camera = cameras.last;
      print(camera);
    }
  
    @override
    void initState() {
      super.initState();
      // getCameras();
      availableCameras().then((availableCameras) {
        cameras = availableCameras;
        camera = cameras.first;
        cameraController = CameraController(
        camera,
        ResolutionPreset.low,
      );
      initializeController = cameraController.initialize();
      print(cameraController.value.isInitialized);
      });
      
      // cameraController = CameraController(
      //   camera,
      //   ResolutionPreset.low,
      // );
      // initializeController = cameraController.initialize();
      // print(cameraController.value.isInitialized);
    }
  
    @override
    void dispose() {
      cameraController.dispose();
      super.dispose();
    }
  
    @override
    Widget build(BuildContext context) {
      return Scaffold(
        body: FutureBuilder&lt;void&gt;(
          future: initializeController,
          builder: (context, snapshot) {
            if (snapshot.connectionState == ConnectionState.done) {
              // If the Future is complete, display the preview.
                return CameraPreview(cameraController);
            }
            else {
              // Otherwise, display a loading indicator.
              print(snapshot.connectionState);
              return Center(child: CircularProgressIndicator());
            }
          },
        ),
  </code></pre>
  <p>I have been relying on <a href="https://flutter.dev/docs/cookbook/plugins/picture-using-camera" rel="nofollow noreferrer">this page</a> to use the camera package, but I could not use it verbatim because I can't keep passing down the camera object down my widget tree.</p>