readFileSync function

String readFileSync(
  1. String filePath
)

Implementation

String readFileSync(String filePath) {
  try {
    final file = File(filePath);
    // Check if file exists before reading
    if (file.existsSync()) {
      // Read the entire file content as a string (UTF-8 encoding by default)
      final contents = file.readAsStringSync();
      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
  }
}