【发布时间】:2019-11-19 06:43:31
【问题描述】:
我在 Flutter 中尝试了一个相机应用程序,遇到了列的子级不能包含空值的问题,但在索引 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 部分检查了有无返回,但问题仍然存在。
我现在已经包含了整个代码。
【问题讨论】:
-
确保 imageFile 是文件系统上的有效位置,然后在 else 条件下返回您的小部件。
-
在 else 语句中返回您的 Image 小部件,您能否展示您的 _showChoiceDialog 方法?