检查 flutter 中是否存在资产 [英] Check if asset exist in flutter
问题描述
如何检查 Flutter 中是否存在特定资产.我正在尝试加载一些图像和声音文件,但我需要处理这些资源不存在的情况.
How I can check if a specific asset exists in Flutter. I'm trying to load some images and sound files and I need to handle the case when these assets do not exist.
我需要检查是否存在,因为我有数字从 1 到 1000 的音频文件和图像.当我构建我的小部件时,我使用从 1 到 1000 的循环来构建它.并且资产中可能不存在所需的文件(当前编号的图像或声音).
I need to check the existence because I have audio files and images for numbers from 1 to 1000. When I build my widgets I use a loop from 1 to 1000 to build it. and there are possibilities that the required file ( the image or the sound for the current number ) does not exist in the assets.
推荐答案
我假设您正在使用 AssetBundle
类使用 load
方法加载数据,该方法采用ByteData
,并且当你使用这个方法时,如果没有找到资产就会抛出异常.
I assume that you are using the AssetBundle
class to load your data using the load
method which takes ByteData
, and when you use this method, it will throws an exception if the asset is not found.
这篇关于检查 flutter 中是否存在资产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!