Check if asset exists Check if asset exists dart dart

Check if asset exists


AssetBundle (as returned by rootBundle) abstracts over different ways of loading assets (local file, network) and there is no general way of checking if it exists.

You can easily wrap your loading code so that it becomes less "ugly".

  Future myLoadAsset(String path) async {    try {      return await rootBundle.loadString(path);    } catch(_) {      return null;    }  } 
var assetPaths = ['file1path', 'file2path', 'file3path'];var asset;for(var assetPath in assetPaths) {  asset = await myLoadAsset(assetPath);  if(asset != null) {    break;   }}if(asset == null) {  throw "Asset and fallback assets couldn't be loaded";}