gpt4 book ai didi

dart - Flutter 应用程序错误 - 在 null 上调用了 getter 'value'

转载 作者:IT王子 更新时间:2023-10-29 06:41:38 26 4
gpt4 key购买 nike

我正在构建一个应用程序,我需要在相机 View 顶部显示一些内容。这是我的代码。

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

无法计算。出了什么错误。

最佳答案

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

关于dart - Flutter 应用程序错误 - 在 null 上调用了 getter 'value',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50304502/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com