Flutter Dio拦截器:DioError [DioErrorType.DEFAULT]:错误状态:无法完成最终的MultipartFile

时间:2020-09-25 06:24:29

标签: flutter dio

嗨,我正在Dio拦截器中尝试refreshtoken逻辑。可以很好地处理json主体参数,但是会抛出 DioError [DioErrorType.DEFAULT]:错误的状态:当我尝试上传图像时,无法最终确定最终的MultipartFile

onError: (DioError error) async {
      // Do something with response error
      if (error.response?.statusCode == 401) {
        // _dio.interceptors.requestLock.lock();
        Response response;
        RequestOptions options = error.response.request;
        response = await _dio
            .post('/user/refresh', data: {"refreshToken": _refreshToken});
    if (response.statusCode == 200) {
      final userData = json.encode(
        {
          'token': response.data["accessToken"],
          'tokenType': _tokenType,
          'refreshToken': response.data["refreshToken"]
        },
      );
      prefs.setString('userData', userData);
      
        options.data = formData;
      }
      options.headers["Authorization"] =
          "$_tokenType ${response.data['accessToken']}";
      return await _dio.request(options.path, options: options);
    } else {
      throw error;
    }

1 个答案:

答案 0 :(得分:0)

我为这个问题制定了一个解决方法,它基本上包括在重试之前重建 FormData。感觉有点hacky,但它有效。我首先通过请求选项中的“额外”映射传递重建所需的任何信息,以便拦截器可以访问它。下面是一些伪代码:

//original request
dioResponse = await dio.post(
  'http://my/api/endpoint',
  data: myOriginalFormData,
  options: Options(
    headers: myHeaders,
    extra: {'pathToMyFile': pathToMyFile},
  ),
);

//and in my interceptor I use it to construct a fresh FormData that has not been finalized
final FormData newFormData = FormData.fromMap({
  'file': await MultipartFile.fromFile(
       requestOptions.extra['pathToMyFile'],
       contentType: MediaType('application/json', 'json')),
});

//retry with the fresh FormData
return dio.request(
  requestOptions.path,
  data: newFormData,
  options: requestOptions,
  cancelToken: requestOptions.cancelToken,
  onReceiveProgress: requestOptions.onReceiveProgress,
  onSendProgress: requestOptions.onSendProgress,
  queryParameters: requestOptions.queryParameters,
);

有人对这种方法有想法吗?有什么主要缺点吗?