downloadCustomLocation method

Future<File?> downloadCustomLocation({
  1. required String? url,
  2. dynamic progress(
    1. String
    )?,
  3. required String filename,
  4. required String extension,
  5. required dynamic path,
})

this is used to download any location in as per user wants in case directory not exist it will create automatically

Implementation

Future<File?> downloadCustomLocation(
    {required String? url,
    Function(String)? progress,
    required String filename,
    required String extension,
    required path}) async {
  Dio dio = new Dio();

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

  String? android_path = path;
  android_path!.printwarn;

  Directory directory = await new Directory(android_path);
  // create directory if not exist
  if (!await directory.exists()) {
    await directory.create(recursive: true);
  }
  directory;

  String fullPath = android_path + filename + "${extension}";
  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);
    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 file;
}