downloadImage static method

Future<File?> downloadImage(
  1. String url,
  2. String path
)

Implementation

static Future<File?> downloadImage(String url, String path) async {
  File? file;
  try {
    final http.Response response = await ApiClient().httpClient.get(Uri.parse(hasDomain(url) ? url : Constants().baseUrlImage + url)).timeout(Duration(seconds: Constants().timeout));
    if (response.statusCode == 200) {
      final List<String> str = url.split('/');
      if (str.length > 1) {
        final Directory folder = Directory(path);
        if (!folder.existsSync()) folder.createSync();
        file = File(path + str[str.length - 1]);
        if (!file.existsSync()) {
          file.createSync();
          file.writeAsBytesSync(response.bodyBytes, flush: true);
        }
      }
    }
  } catch (_) {}
  return file;
}