【发布时间】:2021-08-27 10:34:46
【问题描述】:
我在我的 Flutter 应用中使用 image_picker。
拍完照片后,我将其添加到 XFile 列表中。但是在检查列表长度时,它显示为 null。
当我遵循首先从图库添加图像然后从相机添加图像的路线时,它似乎工作正常。将相机图像添加到列表的正确方法是什么。我的代码如下:
List<XFile> ? _imageFileList;
List<XFile> ? pickedFile;
XFile ? file;
void getImageFile(ImageSource source, bool multimage) async {
if(multimage) {
pickedFile = await _picker.pickMultiImage(maxWidth: double.infinity,
maxHeight: double.infinity, imageQuality: 10);
setState(() {
_imageFileList = pickedFile;
print('image selected');
Navigator.of(context).pop();
Flushbar(
message: 'length of imagelist ${_imageFileList?.length}',
duration: Duration(seconds: 5),
)..show(context);
});
} else{
file = await _picker.pickImage(
source: source, maxWidth: double.infinity,
maxHeight: double.infinity, imageQuality: 10);
setState(() {
pickedFile?.add(file!) ;
_imageFileList?.add(file!) ; //
print('camera image selected');
print('image selected');
Navigator.of(context).pop();
Flushbar(
message: 'length of imagelist ${_imageFileList?.length}',
duration: Duration(seconds: 5),
)..show(context);
});
}
【问题讨论】:
标签: flutter dart imagepicker