downloadData static method

dynamic downloadData(
  1. String url, {
  2. required String savePath,
  3. dynamic onSuccess(
    1. Map? response
    )?,
  4. dynamic onError(
    1. String? error
    )?,
})

Implementation

static downloadData(
  String url, {
  required String savePath,
  Function(Map? response)? onSuccess,
  Function(String? error)? onError,
}) async {
  try {
    Response response;
    Dio? dio = UnitsForNetwork.dio;

    response = await dio!.download(url, savePath);

    ResponseBody responseData = response.data;

    if (responseData.statusCode == 200) {
      if (onSuccess != null) onSuccess(null);
    } else {
      if (onError != null) onError(null);
    }

    debugPrint(responseData.toString());
  } catch (e) {
    debugPrint('请求出错:$e');
    if (onError != null) onError(e.toString());
  }
}