2

I have a synchronous method that reads an image from disk. I'm trying to handle all scenarios where the image didn't get saved properly and is corrupted.

If I place a fake.jpg in my app's documents folder and then run the below code on it, it crashes with: Invalid image data. So the Image.file portion of my code isn't catching an underlying exception.

Widget getPhotoFile(String path) {
  final file = File(path);

  try {
    if (!file.existsSync()) throw Exception('Photo Not Available');

    final photo = Image.file(
      key: UniqueKey(),
      file,
      fit: BoxFit.cover,
    );

    return photo;

  } catch (error) {
    return Text('No Image');
  }
}

Why won't my catch block work for the Image exception and return the Text widget instead?

Clifton Labrum
  • 13,053
  • 9
  • 65
  • 128

0 Answers0