如何处理异常:无法在 flutter 中实例化图像编解码器

How to handle Exception: Could not instantiate image codec in flutter

这是我正在开发的音乐播放器应用程序。我正在使用 audioplayers plugin 来收集所有本地文件。这是我显示缩略图的代码

...Container(
    margin: EdgeInsets.symmetric(horizontal: 40, vertical: 60),
    decoration: BoxDecoration(
      shape: BoxShape.circle,
      color: dark,
      image: DecorationImage(
        image: loadThumbNail(File(path)),
        fit: BoxFit.fitHeight,
      ),
    ),
  ),...

...ImageProvider loadThumbNail(File f) {
ImageProvider img;
  try {
    img = f.existsSync() ? FileImage(f) : AssetImage(path);
  } catch (e) {
    img = AssetImage(path);
  }
 return img;
}

代码有效。如果缩略图不存在,它会加载默认缩略图。 问题似乎只针对一个。 这是怎么回事?

这是错误信息

════════ Exception caught by image resource service ════════════════════════════════════════════════
The following _Exception was thrown resolving an image codec:
Exception: Could not instantiate image codec.

When the exception was thrown, this was the stack: 
#0      _futurize (dart:ui/painting.dart:4419:5)
#1      instantiateImageCodec (dart:ui/painting.dart:1722:10)
#2      PaintingBinding.instantiateImageCodec (package:flutter/src/painting/binding.dart:88:12)
#3      FileImage._loadAsync (package:flutter/src/painting/image_provider.dart:864:24)
<asynchronous suspension>
...
Path: /storage/emulated/0/Android/data/com.android.providers.media/albumthumbs/1572310783227
════════════════════════════════════════════════════════════════════════════════════════════════════

有趣的是应用程序没有崩溃,也没有出现红屏。缩略图区域变成空白。我应该就这样离开还是再深入研究一下?

Thats it I give up. I tried to use onError property, it did work, however the default image remained even for songs with a valid thumbnail.

问题是您提供的路径不是出现此错误的图像路径,如果您使用的是发布应用程序,则不会显示红屏如果资产丢失,Flutter 应用程序不会崩溃。如果您希望它修复,您可以提供更多代码,也可以保持原样。