【问题标题】:how to set dio package in flutter?如何在颤振中设置 dio 包?
【发布时间】:2021-08-22 19:41:28
【问题描述】:

我想创建用于从 Internet 下载文件的 Flutter 应用程序,但是当我使用 dio package 时, 我得到错误;命名参数'onProgress'未定义,我该怎么办?

我的功能

 Future<void> downloadFile() async {
    Dio dio = Dio();

    try {
      var dir = await getApplicationDocumentsDirectory();

      await dio.download(imgUrl, "${dir.path}/myimage.jpg",
          onProgress: (rec, total) {
            print("Rec: $rec , Total: $total");

            setState(() {
              downloading = true;
              progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
            });
          });
    } catch (e) {
      print(e);
    }

    setState(() {
      downloading = false;
      progressString = "Completed";
    });
    print("Download completed");
  }

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    onProgress 替换为onReceiveProgress

    当出现此类问题时,请务必检查函数实现。

    【讨论】:

      【解决方案2】:

      Dio 4.0.0 中download 方法的方法签名如下:

      Future<Response> download(
          String urlPath,
          savePath, {
          ProgressCallback? onReceiveProgress,
          Map<String, dynamic>? queryParameters,
          CancelToken? cancelToken,
          bool deleteOnError = true,
          String lengthHeader = Headers.contentLengthHeader,
          data,
          Options? options,
        });
      

      所以你应该使用onReceiveProgress 而不是onProgress

      背景资料

      onProgress 是创建 Dio 包时使用的初始参数。这个has been changed in the v2.0.14 release。似乎a small part of the initial code docs 自该版本发布以来一直完好无损。

      如果这让您感到困惑,那么在 Dio GitHub 项目上打开一个问题可能是件好事。

      【讨论】:

      • 嗨@flu-tter-87,如果这个或任何答案解决了您的问题,请点击复选标记考虑accepting it。这向更广泛的社区表明您已找到问题的解决方案。
      猜你喜欢
      • 2021-06-10
      • 2020-01-15
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      • 2020-04-24
      • 2021-04-12
      • 2019-05-19
      相关资源
      最近更新 更多