我正在使用Flute音乐播放器插件在Flutter中制作音乐播放器应用。但是我在添加专辑封面方面遇到了麻烦。
我写道:
dynamic getImage(int idx) {
return _songs[idx].albumArt == null
? null
: new File.fromUri(Uri.parse(_songs[idx].albumArt));
}
我使用了Image.file窗口小部件:
Container(
childe: Image.file(getImage(_index))
)
结果是:
I/flutter (15576): The following assertion was thrown building HyperMusicHome(dirty, state:
I/flutter (15576): _HyperMusicHomeState#b83f2):
I/flutter (15576): 'package:flutter/src/painting/image_provider.dart': Failed assertion: line 621 pos 14: 'file !=
I/flutter (15576): null': is not true.
I/flutter (15576):
I/flutter (15576): Either the assertion indicates an error in the framework itself, or we should provide substantially
I/flutter (15576): more information in this error message to help you determine and fix the underlying cause.
I/flutter (15576): In either case, please report this assertion by filing a bug on GitHub:
I/flutter (15576): https://github.com/flutter/flutter/issues/new?template=BUG.md
答案 0 :(得分:1)
您收到的错误来自Flutter的FileImage
类中的this line,该类检查您传递给Image.file()
的内容是否为null
。您当前正在查看的歌曲似乎没有专辑封面。您所要做的就是在没有可用的专辑封面时不显示图像。
我不知道您的小部件到底是什么样子,但是您可以执行以下操作:
@override
Widget build(BuildContext context) {
// Call `getImage` once to get the image file (which might be `null`)
final albumArtFile = getImage(_index);
return Container(
// Only show the image if `albumArtFile` is not `null`
child: albumArtFile != null ? Image.file(albumArtFile) : null,
);
}
您还可以将占位符图像与您的应用捆绑在一起,并在没有可用的专辑封面时显示出来:
albumArtFile != null ? Image.file(albumArtFile) : Image.asset('assets/placeholder.png')
您可以了解有关向应用here添加资产的更多信息。