replaceInFile function

void replaceInFile(
  1. String path,
  2. Pattern from,
  3. String to
)

Replaces all occurrences of from with to in the file at path.

If the file does not exist, this function does nothing.

path - The path to the file to edit. from - The pattern to replace. to - The replacement string.

Implementation

void replaceInFile(String path, Pattern from, String to) {
  final file = File(path);
  if (!file.existsSync()) return;
  final content = file.readAsStringSync();
  file.writeAsStringSync(content.replaceAll(from, to));
}