Home > front end >  Exception: type 'Image' is not a subtype of type 'File?'
Exception: type 'Image' is not a subtype of type 'File?'

Time:02-14

I have a function that returns an image, and another variable initialized as null. but when I call the function and update the state of the variable to get the returned image of the function I get this error :

Unhandled Exception: type 'Image' is not a subtype of type 'File?'

Future ff(String styleImagePath, String originalImagePath) async {
  ImageTransferFacade showtime = ImageTransferFacade();
  showtime.loadModel();
  var style_image =
      await showtime.loadStyleImage('assets/Tableau_Dashboard.jpg');
  var original_image =
      await showtime.loadoriginalImage('assets/Tableau_Dashboard.jpg');

  var output_image = await showtime.transfer(original_image, style_image);
  output_image = Image.memory(output_image);
  return output_image;
}

File? finalsd = null;
// This is where I'll use the 'finalsd' variable , but if I change type to Image instead of File
    child: Card(
                      child: (finalsd == null)
                          ? Image.file(File(_image.path))
                          : Image.file(File(finalsd!
                              .path)))))

Error emerges here :

child: GestureDetector(
                  onTap: () {
                    ff(imageList[index], widget.image.path).then((value) {
                      setState(() {
                        finalsd = value;

CodePudding user response:

Your method returns a Future containing the type Image. After resolving the Future in your GestureDetector you are assigning it to a variable of type File. Both types are not related and thus the error is thrown.

output_image = Image.memory(output_image);
return output_image;

This creates and returns an Image and you are assigning it to this variable:

File? finalsd = null;

Dart would probably also warn you about this if you would declare the generic type in the return type of your method:

Future<Image> ff(String styleImagePath, String originalImagePath) async {
  • Related