gpt4 book ai didi

dart - Flutter Camera 插件错误 - 在 null 上调用了 getter 'height'

转载 作者:IT王子 更新时间:2023-10-29 07:10:14 29 4
gpt4 key购买 nike

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

最佳答案

即使您在 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 替换为空容器。

关于dart - Flutter Camera 插件错误 - 在 null 上调用了 getter 'height',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50306436/

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