readTextFile static method

Future<String?> readTextFile(
  1. String folderPath,
  2. String fileName
)

Reads the contents of a text file with the given fileName (without extension) from the specified folderPath. The file is expected to have a .txt extension.

Returns the file contents as a String, or null if the file or folder does not exist.

Implementation

static Future<String?> readTextFile(String folderPath, String fileName) async {
  final targetDir = Directory(folderPath);
  if (!await targetDir.exists()) {
    return null;
  }
  String filePath = "${targetDir.path}/$fileName.txt";
  final file = File(filePath);

  if (await file.exists()) {
    return await file.readAsString();
  }
  return null;
}