replaceWithinFile method Null safety

Future<File?> replaceWithinFile(
  1. String path,
  2. Pattern from,
  3. String to
)

Replace contents of file

Implementation

Future<File?> replaceWithinFile(String path, Pattern from, String to) async {
  final file = File(p.join('lib', 'app', path));
  final fileExists = await file.exists();
  if (!fileExists) return null;

  final contents = await file.readAsString();
  final replacedContents = contents.replaceAll(from, to);
  final writtenFile = await file.writeAsString(replacedContents);
  return writtenFile;
}