Search code examples
flutterdartflutter-image-picker

How can I fix the error a value of type 'XFile?' can't be assigned to a variable of type 'File'


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 };
      });
    }
  }

Solution

  • You can get file like

    XFile? picture = await ImagePicker()
        .pickImage(source: ImageSource.camera, imageQuality: 75);
    if (picture != null) {
      final File imageFile = File(picture.path);
    }