copyFile function

Future<void> copyFile(
  1. File file,
  2. String path
)

Implementation

Future<void> copyFile(File file, String path) async {
  final newPath = file.absolute.path.replaceFirst(file.path, path);

  final newFile = File.fromUri(Uri.file(newPath));

  if (!await newFile.exists()) {
    await newFile.create(recursive: true);
  }

  final stream = file.openRead();

  final sink = newFile.openWrite();

  await for (final bytes in stream) {
    sink.add(bytes);

    await sink.flush();
  }

  await sink.close();
}