downloadImage static method

Future<String> downloadImage(
  1. String imgUrl, {
  2. String imgName = '',
})

Implementation

static Future<String> downloadImage(String imgUrl, {String imgName = ''}) async {
  AppConfig.logger.d("Entering downloadImage method");
  String localPath = "";
  String name = imgName.isNotEmpty ? imgName : imgUrl;
  try {

    final response = await http.get(Uri.parse(imgUrl));
    if (response.statusCode == 200) {
      name = name.replaceAll(".", "").replaceAll(":", "").replaceAll("/", "");
      // Get the document directory path
      localPath = await FileSystemUtilities.getLocalPath();
      localPath = "$localPath/$name.jpeg";
      File jpegFileRef = File(localPath);
      await jpegFileRef.writeAsBytes(response.bodyBytes);
      AppConfig.logger.i("Image downloaded to path $localPath successfully.");
    }
  } catch (e) {
    AppConfig.logger.e(e.toString());
  }
  return localPath;
}