Dio:取消通话时。请求未取消

时间:2019-09-18 12:25:07

标签: flutter dart

我在使用Dio api客户端取消请求时遇到问题。

        final cancelToken = CancelToken();

        final request = host.request(path,
            data: data,
            queryParameters: query,
            options: Options(method: describeEnum(method)),
            cancelToken: cancelToken);

        final cancelableRequest = CancelableOperation.fromFuture(request, onCancel: () {
          cancelToken.cancel();
        });

当cancelToken.cancel();时称为我收到DioErrorType.CANCEL,但请求未取消(在Charles Web代理中选中)。我已经尝试过节流请求,但它总是等待响应。

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这样写,取消Dio api调用

CancelToken cancelToken = CancelToken();

Request request = host.request(path,
  data: data,
  queryParameters: query,
  options: Options(method: describeEnum(method)),
  cancelToken: cancelToken);

// cancel the requests with "cancelled" message.
final cancelableRequest = CancelableOperation.fromFuture(request, onCancel: token.cancel("cancelled"));