【问题标题】:Flutter app error - getter 'value' was called on nullFlutter 应用程序错误 - 在 null 上调用了 getter 'value'
【发布时间】:2018-10-22 14:09:28
【问题描述】:

我正在构建一个应用程序,我需要在相机视图顶部显示一些东西。这是我的代码。

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

List<CameraDescription> cameras;

void main() async {
  runApp(new MaterialApp(
    home: new CameraApp(),
  ));

}

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

class _CameraAppState extends State<CameraApp> {
  CameraController controller;
  var cameras;
  bool cameraGot = false;

  Future<Null> getCamera() async {
    cameras = await availableCameras();
    setState(() {
      this.cameras = cameras;
      this.cameraGot = true;
    });
  }

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

    if(this.cameraGot) {
      controller = new CameraController(this.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>[
          !this.controller.value.initialized ? new Container() : cameraView,

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

        ],
      ),
    );
  }
}

每当我构建应用程序时,我都会遇到以下错误...

I/flutter ( 6911): ══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
I/flutter ( 6911): The following NoSuchMethodError was thrown building CameraApp(dirty, state: _CameraAppState#b6034):
I/flutter ( 6911): The getter 'value' was called on null.
I/flutter ( 6911): Receiver: null
I/flutter ( 6911): Tried calling: value

想不通。出了什么错误。

【问题讨论】:

标签: dart flutter


【解决方案1】:

如果您已创建变量并为其赋值,但它仍然显示getter 'value' was called on null, 尝试RunRestart 你的应用程序而不是Hot Reload。因为Hot Reload 不会调用initstate()(变量分配它们的值),而Restarting 只会调用应用程序。

【讨论】:

    【解决方案2】:

    initState 中,您并不总是实例化控制器。

    这意味着它可以是null。所以在build方法里面,你需要检查null才不会崩溃

    (!this.controller?.value?.initialized ?? false) ? new Container() : cameraView ,
    

    【讨论】:

    • 我试过了,但还是遇到了同样的错误。
    • 您使用控制器的地方不止一处。你需要到处检查一下
    • 在这一行,我也做出了类似你aspectRatio: (this.controller?.value?.aspectRatio) ?? false 的更改,但仍然没有运气。现在,错误显示是``` type 'bool' is not a subtype of type 'double' where```
    • 是控制器吗?。值?选项就像 swift 一样吗? @RémiRousselet
    【解决方案3】:
    aspectRatio: (this.controller?.value?.aspectRatio) ?? false
    

    aspectRatio 是一个双精度值,当它是 null 时,您将它分配给 false。给它一个双精度值。

    【讨论】:

      【解决方案4】:

      我之前遇到过这个问题,并且在AspectRatio 的父小部件中设置了一个高度来解决它。

      【讨论】:

        【解决方案5】:

        为抛出错误的小部件提供高度解决了我的问题

        【讨论】:

          【解决方案6】:

          我是这样解决问题的:

          children: <Widget>[
              (controller== null)
                                ? Container()
                                : controller.value.Initialized
                                    ? Container(child: CameraPreview(controller))
                                    : Container(),
              ],
          

          【讨论】:

            【解决方案7】:

            大多数时候。 HOT RESTART (R) 将修复此错误。

            【讨论】:

            • 请详细说明您的答案。试着举一些好的例子或正确解释。
            • 这只会重启应用,不会修复错误!
            • 这会消除错误,但在我的情况下,当您启动应用程序时再次出现相同的错误
            • 在 Cupertino 产品应用程序 Flutter 教程中出现问题时工作。
            猜你喜欢
            • 2020-03-24
            • 2019-01-09
            • 2020-10-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-10-22
            • 2021-08-11
            • 2020-01-08
            相关资源
            最近更新 更多