gpt4 book ai didi

flutter - Flutter错误Column的子级不能包含任何空值,但是在索引0处找到了空值

转载 作者:行者123 更新时间:2023-12-03 03:12:13 24 4
gpt4 key购买 nike

我在尝试使用照相机应用程序时遇到问题,即列的子代不能包含空值,但是在索引0处找到了空值。我无法在列的子代中传递小部件。

import 'dart:io';
import 'package:flutter/material.dart';
import 'package:image_picker/image_picker.dart';

void main() {
runApp(new MaterialApp(
title: "Camera App",
home: LandingScreen(),
));
}

class LandingScreen extends StatefulWidget {
@override
_LandingScreenState createState() => _LandingScreenState();
}

class _LandingScreenState extends State<LandingScreen> {

File imageFile;

_openGallery(BuildContext context) async {
var picture = await ImagePicker.pickImage(source: ImageSource.gallery);
this.setState((){
imageFile = picture;
});
Navigator.of(context).pop();
}

_openCamera(BuildContext context) async{
var picture = await ImagePicker.pickImage(source: ImageSource.camera);
this.setState((){
imageFile = picture;
});
Navigator.of(context).pop();
}

Future<void> _showChoiceDialog(BuildContext context){
return showDialog(context: context,builder: (BuildContext context){
return AlertDialog(
title: Text("Make a choice!"),
content: SingleChildScrollView(
child: ListBody(
children: <Widget>[
GestureDetector(
child: Text("Gallery"),
onTap: (){
_openGallery(context);
},
),
Padding(padding: EdgeInsets.all(8.0),),
GestureDetector(
child: Text("Camera"),
onTap: (){
_openCamera(context);
},
),
],
),
),
);
});
}

Widget _decideImageView(){
if(imageFile == null){
return Text("No Image Selected");
}
else{
return Image.file(imageFile, width: 400, height: 400,);
}
}

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Main Screen"),
),
body: Container(
child: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.spaceAround,
children: <Widget>[
_decideImageView(),
RaisedButton(onPressed: (){
_showChoiceDialog(context);
},child: Text("Select Image!"),)
],
)
)
)
);
}
}

我在else部分中检查了是否返回,但问题仍然存在。

我现在已经包括了整个代码。

最佳答案

您必须在其他情况下返回Image Widget:

Widget _decideImageView(){
if(imageFile == null){
return Text("No Image Selected");
}
else{
return Image.file(imageFile, width: 400, height: 400,);
}
}

关于flutter - Flutter错误Column的子级不能包含任何空值,但是在索引0处找到了空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58928061/

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