saveToFile function

Future<String> saveToFile(
  1. String content,
  2. String fileName, {
  3. String? folder,
})

Saves content to a file fileName in optional folder.

Creates the file if it does not exist.

Returns the full path of the saved file.

Implementation

Future<String> saveToFile(String content, String fileName,
    {String? folder}) async {
  final String directory = await getStorageFolderPath();
  final dir = Directory(
      "$directory${folder != null ? '${Platform.pathSeparator}$folder' : ''}");
  if (!(await dir.exists())) {
    await dir.create();
  }
  final file = File("${dir.path}${Platform.pathSeparator}$fileName");
  if (!(await file.exists())) {
    await file.create();
  }
  await file.writeAsString(content);
  return file.path;
}