downloadFile static method

Future<String> downloadFile(
  1. String path,
  2. String saveToPath, {
  3. Map<String, String>? headers,
  4. OnSuccess? onSuccess,
  5. OnError? onError,
})

Downloads content from path and saves it to saveToPath.

Returns a success message or throws on error.

Implementation

static Future<String> downloadFile(
  String path,
  String saveToPath, {
  Map<String, String>? headers,
  OnSuccess? onSuccess,
  OnError? onError,
}) async {
  try {
    final response = await http.get(
      Uri.parse('$baseUrl$path'),
      headers: {...defaultHeaders, ...?headers},
    );

    if (response.statusCode == 200) {
      final file = File(saveToPath);
      await file.writeAsBytes(response.bodyBytes);
      final message = 'File saved to $saveToPath';
      onSuccess?.call(message);
      return message;
    } else {
      throw FinnUtilsClientException('Download failed: ${response.body}');
    }
  } catch (e) {
    onError?.call(FinnUtilsClientException(e.toString()));
    rethrow;
  }
}