Flutter. Check if a file exists before loading it Flutter. Check if a file exists before loading it dart dart

Flutter. Check if a file exists before loading it


In order to see whether or not a file exists in internal local storage of the app use:

import 'dart:io' as io;var syncPath = await path;// for a fileio.File(syncPath).exists();// for a directoryio.Directory(syncPath).exists();


For me simply worked this:

import 'dart:io';File("path/to/file").exists() 

or, for checking it synchronously

import 'dart:io';File("path/to/file").existsSync()


Looks like you want to try to load an ImageProvider from a folder where the image may or may not exist and then, if it does not, load a fallback asset image (which you can be sure will exist as you'll put it in your root bundle).

Try this:

ImageProvider getImageProvider(File f) {  return f.existsSync()      ? FileImage(f)      : const AssetImage('images/fallback.png');}