downloadFile method

void downloadFile(
  1. String url,
  2. Map<String, String>? headers,
  3. Map<String, dynamic>? queryParams,
  4. String? savePath,
)

Implementation

void downloadFile(String url, Map<String, String>? headers, Map<String, dynamic>? queryParams, String? savePath) async {
  var finalURL = url.replaceAll("//v2//", "/v2/"); //@todo
  final stringQueryParams = queryParams?.map((key, value) => MapEntry(key, value.toString()));
  final uri = Uri.parse(finalURL);
  final uriWithParams = uri.replace(queryParameters: stringQueryParams);
  printCurlCommand(finalURL,headers,queryParams);

  final response = await http.get(uriWithParams, headers: headers);

  final blob = html.Blob([response.bodyBytes]);
  final blobUrl = html.Url.createObjectUrlFromBlob(blob);
  final anchor = html.AnchorElement(href: blobUrl)
    ..setAttribute('download', savePath ?? "arquivo")
    ..click();
}