Suresh
Suresh

Reputation: 5997

Flutter Camera plugin error - The getter 'height' was called on null

I'm working on a Camera app. I'm using the following Camera plugin - https://github.com/flutter/plugins/tree/master/packages/camera

Here is my working code -

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';

List<CameraDescription> cameras;

Future<Null> main() async {
  cameras = await availableCameras();
  runApp(new MaterialApp(
    home: new CameraApp(),
  ));

}

class CameraApp extends StatefulWidget {
  @override
  _CameraAppState createState() => new _CameraAppState();
}

class _CameraAppState extends State<CameraApp> {
  CameraController controller;

  @override
  void initState() {
    super.initState();

    controller = new CameraController(cameras[0], ResolutionPreset.medium);
    controller.initialize().then((_) {
      if (!mounted) {
        return;
      }
      setState(() {});
    });
  }

  @override
  void dispose() {
    controller?.dispose();
    super.dispose();
  }


  @override
  Widget build(BuildContext context) {

    // camera widget
    Widget cameraView = new Container(
      child: new Row(children: [
          new Expanded(
            child: new Column(
                children: <Widget>[
                  new AspectRatio(
                    aspectRatio: controller.value.aspectRatio,
                    child: new CameraPreview(controller)
                  )
                ]
            ),
          )
      ])
    );

    return new Scaffold(
      body: new Stack(
        children: <Widget>[
          (!controller.value.initialized) ? new Container() : cameraView,

           // ---On top of Camera view add one mroe widget---

        ],
      ),
    );
  }
}

When I'm building the app I'm getting following errors...

I/flutter ( 2097): The following NoSuchMethodError was thrown building CameraApp(dirty, state: _CameraAppState#a0666):
I/flutter ( 2097): The getter 'height' was called on null.
I/flutter ( 2097): Receiver: null
I/flutter ( 2097): Tried calling: height

Upvotes: 1

Views: 3644

Answers (2)

I&#39;m a Believer
I&#39;m a Believer

Reputation: 9

if even after using this check (!controller.value.initialized) ? new Container() : cameraView,still you are getting error that "getter 'height' was called on null", and the error message is poping on your app only for fraction of second, then it means you are initializing your camera controller in didChangeDependencies()...if yes then use this technique.

bool cameraInitialized = false;
  @override
  void didChangeDependencies() {
    if (cameraInitialized == false) {
     
      final ScreenArguments arguments =
          ModalRoute.of(context).settings.arguments;

      int cameraIndex = Provider.of<XYZ>(context)
          .XX
          .firstWhere((element) => element.id == arguments.XX`enter code here`Id)
          .cameraIndex;
      controller = new CameraController(
          widget.cameras[cameraIndex], ResolutionPreset.medium);
      controller.initialize().then((value) {
        if (!mounted) {
          return;
        }
        setState(() {});
      });
     

    setState(() {
            cameraInitialized = true;
          });


    }
    super.didChangeDependencies();
  }

Upvotes: 0

Richard Heap
Richard Heap

Reputation: 51751

Even though you have the ternary operator inside the body of the Stack, you are creating the Widget cameraView regardless of whether it is going to be used - so it is being created whether controller.value.initialized is true or false. Adjust the code so that the CameraPreview tree is only built if it is needed, i.e. if initialized is true. For example:

  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      body: new Stack(
        children: <Widget>[
          (!controller.value.initialized) ? new Container() : buildCameraView(),

          // ---On top of Camera view add one mroe widget---
        ],
      ),
    );
  }

  Widget buildCameraView() {
    return new Container(
      child: new Row(
        children: [
          new Expanded(
            child: new Column(
              children: <Widget>[
                new AspectRatio(
                  aspectRatio: controller.value.aspectRatio,
                  child: new CameraPreview(controller),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

As you suggest in your comment, you can move the ternary operator lower in your build tree, too, and replace just the AspectRatio with an empty Container.

Upvotes: 6

Related Questions