downloadFile method
Downloads a file from a remotePath
to a localPath
on the device.
Implementation
@override
Future<String> downloadFile(
{required String remotePath, required String localPath}) {
return _executeRequest(() async {
final normalizedPath = _normalizePath(remotePath);
debugPrint(
'Downloading from Dropbox path: $normalizedPath to $localPath');
final response = await _dio.post(
'https://content.dropboxapi.com/2/files/download',
options: Options(
headers: {
'Dropbox-API-Arg': jsonEncode({'path': normalizedPath})
},
responseType: ResponseType.stream, // Download as a stream.
),
);
final file = File(localPath);
final sink = file.openWrite();
final Stream<Uint8List> stream = response.data.stream;
await stream.cast<List<int>>().pipe(sink); // Pipe stream to file.
debugPrint('Successfully downloaded file to $localPath');
return localPath;
});
}