readFileAsync function

Future<String> readFileAsync(
  1. String filePath
)

Implementation

Future<String> readFileAsync(String filePath) async {
  try {
    final file = File(filePath);
    // Check if file exists before reading
    if (await file.exists()) {
      // Read the entire file content as a string (UTF-8 encoding by default)
      final contents = await file.readAsString();
      return contents;
    } else {
      // Handle file not found error
      print('Error: File not found at $filePath');
      return ''; // Or throw an exception
    }
  } catch (e) {
    // Handle potential errors during file reading
    print('Error reading file $filePath: $e');
    return ''; // Or throw an exception
  }
}