overwriteHtmlFile method

Future<String> overwriteHtmlFile(
  1. String htmlString
)

Implementation

Future<String> overwriteHtmlFile (String htmlString) async {
  // Get the application's document directory
  final directory = await getApplicationDocumentsDirectory();

  // Create a specific folder for caching EPUB files
  final epubHtml = Directory('${directory.path}/epub_html');
  if (!await epubHtml.exists()) {
    await epubHtml.create(recursive: true);
  }

  // Define the file path
  final filePath = '${epubHtml.path}/$title.html';

  final file = File(filePath);
    // Save the file if it doesn't exist
  if (!await file.exists()) {
    //  delete old file and add new
    await file.delete();
    await file.create(recursive: true);
    file.writeAsStringSync(htmlString);
  } else{
    file.writeAsStringSync(htmlString);
  }// end if-else
  //  CLEAR UP MEMORY
  epubBookRef!.closeBook();
  //  ENSURE GC INIT
  htmlFilesInMemory.add(filePath);
  return filePath;
}