【问题标题】:Flutter Camera plugin error - The getter 'height' was called on nullFlutter Camera 插件错误 - 在 null 上调用了 getter 'height'
【发布时间】:2018-10-22 16:53:51
【问题描述】:

我正在开发一个相机应用程序。我正在使用以下相机插件 - 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

【问题讨论】:

    标签: dart flutter


    【解决方案1】:

    即使您在 Stack 的主体中有三元运算符,您也正在创建 Widget cameraView,而不管它是否将被使用 - 因此无论 controller.value.initialized 是真还是假,它都会被创建。调整代码,以便仅在需要时构建 CameraPreview 树,即如果 initialized 为真。例如:

      @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 替换为空 Container。

    【讨论】:

    • 这条线(!controller.value.initialized) ? new Container() : cameraView,不会做同样的事情吗?
    • 查看编辑后的答案 - CameraPreview 即使不需要也正在构建中
    • 嗨,感谢您的回复和指导我关于CameraPreview 的事情。我通过使用这条线 (!controller.value.initialized) ? new Container() : new AspectRatio(aspectRatio: controller.value.aspectRatio,child: new CameraPreview(controller)) 修复了这个问题。
    • 但是,为什么你的示例代码和我已经在帖子中分享的我的一样。事实上,您的main() 示例代码也与我已经发布的相同!!!无论如何,我接受你的帖子作为答案。如果您在帖子中添加我的解决方案行,那就太好了。这样,如果将来有人遇到此问题,其他人可以轻松获得解决方案。
    • 感谢您对 main() 的评论 - 删除了多余的 MaterialApp。您建议您可以在任何地方插入三元运算符是对的;我想让您的代码结构尽可能接近原始结构。重点仍然是原始代码在测试控制器是否已初始化之前尝试构造 AspectRatio。
    【解决方案2】:

    如果即使在使用此检查 (!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();
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      • 2020-01-08
      • 2021-12-10
      • 2020-03-24
      • 2021-05-25
      • 2023-03-08
      相关资源
      最近更新 更多