The error is this : A value of type ‘XFile?’ can’t be assigned to a variable of type ‘File’. I tried changing the type of the variable "picture", from File to XFile, and it gives me this error instead: A value of type ‘XFile?’ can’t be assigned to a variable of type ‘XFile’.
This is my code so far:
void _openCamera() async {
if(imagenes.length == 8) {
alerta(contextMain, 'Limites de imagenes');
}
else {
File picture = await ImagePicker.pickImage(source: ImageSource.camera, imageQuality: 75);
int pos = imagenes.length;
imagenes.add({pos:picture});
setState(() {});
await jsonBloc.cargarImagen( picture ).then((value) {
int id = value;
imagenes[pos] = { id: picture };
});
}
}
3
Answers
ImagePicker.pickImage
is a Future that returns anXFile?
, which means it can either return an actualXFile
, ornull
.So your variable
picture
needs to be anXFile?
as well. So keep in mind in the rest of your code thatpicture
can benull
.You can get file like
This happens because the package (image_picker ) you are using is relying on XFile and not File, as previously done.
So, first, you have to create a variable of type File so you can work with it later as you did, and after fetching the selectedImage you pass the path to instantiate the File. Like this: