Search code examples
flutterdartflutter-layout

The argument type 'XFile' can't be assigned to the parameter type 'File'


Please, can anyone help rewrite this code, don't know what I'm getting wrong.

 Widget _decideImageView(){
        
        if(imageFile == null) {
        return Text("No Image Selected!");} 
        else {
        Image.file(imageFile!,width: 400,height: 400,);
        }
        return Text("No Image Selected!");
  }

Solution

  • Change File? imageFile; to XFile? imageFile;

    Use Image.file(File(imageFile!.path), from dart:io

     XFile? imageFile;
      _openCamera(BuildContext context) async {
        imageFile = await ImagePicker().pickImage(source: ImageSource.camera);
        setState(() {});
        Navigator.of(context).pop();
      }
    
      Widget _decideImageView() {
        if (imageFile == null) {
          return Text("No Image Selected!");
        } else {
          return Image.file(
            File(imageFile!.path),
            width: 400,
            height: 400,
          );
        }
      }