downloadFileIos method

Future<File?> downloadFileIos({
  1. String? url,
  2. dynamic progress(
    1. String
    )?,
  3. StorageDirectory? storageDirectory,
})

Implementation

Future<File?> downloadFileIos(
    {String? url,
    Function(String)? progress,
    pp.StorageDirectory? storageDirectory}) async {
  Dio dio = new Dio();

  if (url == null) {
    "Download Location cant be null".printerror;
    return null;
  }

  Directory? tempDir = await pp.getExternalStorageDirectory();

  String? urlFileType = getUrlFileExtension(url);

  String fullPath = tempDir!.path + "/${Uuid().v4()}${urlFileType}";
  fullPath.printinfo;
  File file = new File("");
  try {
    Response response = await dio.get(
      url,
      onReceiveProgress: (received, total) {
        if (total != -1) {
          if (progress != null)
            progress((received / total * 100).toStringAsFixed(0));
        }
      },
      //Received data with List<int>
      options: Options(
          responseType: ResponseType.bytes,
          followRedirects: false,
          validateStatus: (status) {
            return status! < 500;
          }),
    );
    print(response.headers);
    file = File(fullPath);
    fullPath.printwarn;
    var raf = file.openSync(mode: FileMode.write);
    // response.data is List<int> type
    raf.writeFromSync(response.data);
    await raf.close();
  } catch (e) {
    print(e);
    return null;
  }
  return file;
}