Flutter Camera插件错误 - 吸气器的高度'在null上调用

时间:2018-05-12 12:49:22

标签: dart flutter

我正在使用相机应用。我使用了以下相机插件 - https://github.com/flutter/plugins/tree/master/packages/camera

这是我的工作代码 -

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---

        ],
      ),
    );
  }
}

当我构建应用时,我遇到了以下错误......

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

2 个答案:

答案 0 :(得分:3)

即使你在堆栈正文中有三元运算符,你也会创建Widget cameraView,无论它是否会被使用 - 所以无论controller.value.initialized是真还是正在创建它假。调整代码,以便仅在需要时构建CameraPreview树,即如果initialized为true。例如:

  @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),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

正如您在评论中建议的那样,您也可以在构建树中移动三元运算符,并将AspectRatio替换为空容器。

答案 1 :(得分:0)

如果即使在使用此检查 (!controller.value.initialized) ? new Container() : cameraView 之后,您仍然收到“getter 'height' was called on null”的错误消息, 并且错误消息仅在您的应用程序上弹出几分之一秒,那么这意味着您正在 didChangeDependencies() 中初始化您的相机控制器...如果是,则使用此技术。

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();
  }