当用户进入页面时,我直接打开相机。它会打开,Iam可以拍摄并显示图像。但是我在打开相机之前以及关闭相机以显示拍摄的图像而不是相机时收到错误消息“类型'Future'不是类型'Widget'的子类型”。我知道这是因为Iam使用异步功能来打开相机并获取图像文件。但是我无法解决此错误。有人可以帮我解决这个问题吗?
这是我在有状态小部件中的代码:
File _imageFile;
_getLocalImage() async {
final imageFile = await ImagePicker().getImage(source: ImageSource.camera, imageQuality: 100);
if (imageFile != null) {
setState(() {
_imageFile = File(imageFile.path);
});
} else {
Navigator.of(context).pop();
}
}
@override
Widget build(BuildContext context) {
double _width = MediaQuery.of(context).size.width;
double _height = MediaQuery.of(context).size.height;
return _imageFile == null? _getLocalImage() :
Image.file(
_imageFile,
fit: BoxFit.cover,
height: _height - 20,
width: _width,
);
}