download function

Future<List<int>> download(
  1. String image
)

Implementation

Future<List<int>> download(String image) async {
  final dio = Dio();
  final response = await dio.get<dynamic>(
    image,
    onReceiveProgress: (count, total) {
      if (total != -1) {
        stdout.writeln(
          'Downloading... ${(count / total * 100).toStringAsFixed(0)}%',
        );
      }
    },
    options: Options(
      responseType: ResponseType.bytes,
      followRedirects: false,
      validateStatus: (status) {
        return (status ?? 0) < 500;
      },
    ),
  );

  if (response.statusCode == 200) {
    return response.data;
  } else {
    stdout.writeln(response.statusMessage);
    return [];
  }
}