Home > Enterprise >  How to create specific file if it doesnt exist in flutter
How to create specific file if it doesnt exist in flutter

Time:09-19

i have a question, i want to open a file using

getPath() async {
  Directory directory = await getApplicationDocumentsDirectory();
  String path = directory.path;
  return path;
}

/.../

var path = await getPath();

File file = ("${path}/data.txt") // if data.txt doesnt exist??

this function. The issue is that, if the file doesnt exist, i get this error: OS Error: The system cannot find the file specified.

So, how can i create that file if it doesnt exist?

Thanks a lot

CodePudding user response:

Check if the file exists and if it doesnt you can create it with the create method

bool doesFileExists = await file.exists();
if(!doesFileExists){
  file.create();
}
  • Related