从图像选择器获取图像时,方法 'add' 在 null 上被调用
The method 'add' was called on null while getting image from image picker
我正在通过图像选择器获取图像,然后将文件添加到文件列表,但它显示错误-方法 'add' 被调用为 null。.
我的代码:-
final picker=ImagePicker();
selectImageFromGallery() async
{
setState(() {
inProcess=true;
});
final imageFile= await picker.getImage(source: ImageSource.gallery);
if(imageFile!=null)
{
File _image=File(imageFile.path);
files.add(_image);
}
setState(() {
inProcess=false;
});
}
文件似乎是 List<File>
但未正确初始化。
需要初始化为空列表,如:
final files = <File>[];
这样您就可以向其中添加文件。
我正在通过图像选择器获取图像,然后将文件添加到文件列表,但它显示错误-方法 'add' 被调用为 null。. 我的代码:-
final picker=ImagePicker();
selectImageFromGallery() async
{
setState(() {
inProcess=true;
});
final imageFile= await picker.getImage(source: ImageSource.gallery);
if(imageFile!=null)
{
File _image=File(imageFile.path);
files.add(_image);
}
setState(() {
inProcess=false;
});
}
文件似乎是 List<File>
但未正确初始化。
需要初始化为空列表,如:
final files = <File>[];
这样您就可以向其中添加文件。