replaceFileContent static method

Future<void> replaceFileContent({
  1. required String filePath,
  2. required String newContent,
  3. bool createIfNotExists = false,
})



Implementation

static Future<void> replaceFileContent({
  required String filePath,
  required String newContent,
  bool createIfNotExists = false,
}) async {
  try {
    final file = File(filePath);

    if (createIfNotExists == false) {
      if (!await file.exists()) {
        // await file.create(recursive: true);
        throw FormatException('❌ File not found: $filePath');
      }
    } else {
      await file.create(recursive: true);
    }

    await file.writeAsString(newContent);
  } catch (e) {
    rethrow;
  }
}