Пакет Dart Dio «Ошибка «»запрос» Объект не определен

#dart #dio

Вопрос:

В документации по пакету Dart Dio по адресу https://pub.dev/packages/dio#handling-errors в нем описано, как обрабатывать ошибки:

 try {
  //404
  await dio.get('https://wendux.github.io/xsddddd');
} on DioError catch (e) {
  // The request was made and the server responded with a status code
  // that falls out of the range of 2xx and is also not 304.
  if (e.response) {
    print(e.response.data)
    print(e.response.headers)
    print(e.response.request)
  } else {
    // Something happened in setting up or sending the request that triggered an Error
    print(e.request)
    print(e.message)
  }
}
 

Код имеет смысл и дает мне возможности, необходимые для понимания того, что произошло с моим запросом, но инструмент анализа Dart в Android Studio (я работаю над приложением Flutter) бурно реагирует на него.

Я могу исправить многие жалобы анализатора, добавив нулевые проверки в код, как рекомендовано Android Studio:

 try {
    var response = await dio.get(url, options: options);
    print('Response: $response');
    return response.data;
} on DioError catch (e) {
    // The request was made and the server responded with a status code
    // that falls out of the range of 2xx and is also not 304.
    if (e.response != null) {
      print(e.response!.data);
      print(e.response!.headers);
      print(e.response!.request);  <-- line 64
    } else {
      // Something happened in setting up or sending the request that triggered an Error
      print(e.request);  <-- line 67
      print(e.message);
    }
}
 

но анализатор все равно жалуется на request объект:

 error: The getter 'request' isn't defined for the type 'Response<dynamic>'. (undefined_getter at [particle_cloud] libsrcparticle.dart:64)
error: The getter 'request' isn't defined for the type 'DioError'. (undefined_getter at [particle_cloud] libsrcparticle.dart:67)
 

Я предполагаю, что в ошибке DioError должен быть определен соответствующий объект запроса, как мне исправить этот код, чтобы он выполнялся?

Ответ №1:

Их readme не обновлялся при изменении API. Эквивалентом request в новом API является requestOptions . Это можно легко найти, посмотрев ссылку на API.

 print(e.requestOptions);
 

Комментарии:

1. Спасибо. Это сработало. Я отправил PR с командой Dio, чтобы исправить документы.