Toey
Toey

Reputation: 11

can't check exists file in flutter but can check exists file in dart

in dart enter image description here

in flutter enter image description here

add file in pubspec.yaml enter image description here

I want to check exists file in flutter. Where do I need to modify the code?

Upvotes: 0

Views: 523

Answers (1)

Josteve Adekanbi
Josteve Adekanbi

Reputation: 12673

Try this:

Future<bool> assetExists(String path) async{
 try{
   await rootBundle.loadString(path);
   return true;
 } catch (e){
   return false;
 }
}

Then you can use the method like so:

String path = 'images/nocolor.png';
bool fileExists = await assetExists(path);
if (fileExists) {
   print ("have file");
}else{
   print ("on file");
}

Upvotes: 1

Related Questions