imageDownloadProgress static method

Stream<String> imageDownloadProgress(
  1. String url
)

This function is returning the image downloading progress.

Implementation

static Stream<String> imageDownloadProgress(String url) async* {
  StreamController<String> streamController = new StreamController();
  try {
    final dir = await getTemporaryDirectory();
    Dio dio = new Dio();
    dio
        .download(
          url,
          "${dir.path}/myimage.jpeg",
          onReceiveProgress: (int received, int total) {
            streamController
                .add(((received / total) * 100).toStringAsFixed(0) + "%");
          },
        )
        .then((Response response) {})
        .catchError((ex) {
          streamController.add(ex.toString());
        })
        .whenComplete(() {
          streamController.close();
        });
    yield* streamController.stream;
  } catch (ex) {
    throw ex;
  }
}