johnwargo
johnwargo

Reputation: 797

Dart Dio Package `DioError` `request` Object not defined

In the Dart Dio package documentation at https://pub.dev/packages/dio#handling-errors it describes how to handle 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)
  }
}

The code makes sense and gives me the options I need to understand what happened with my request, but the Dart Analysis tool in Android Studio (I'm working on a Flutter app) reacts violently to it.

I can fix many of the analyzer's complaints by adding null checks to the code as recommended by 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);
    }
}

but the analyzer still complains about the request object:

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

I'm assuming the DioError should have the appropriate request object defined, how do I get this code fixed so it runs?

Upvotes: 2

Views: 1587

Answers (1)

Christopher Moore
Christopher Moore

Reputation: 17141

Their readme was not updated when the API changed. The equivalent to request in the new API is requestOptions. This can be easily found by looking in the API reference.

print(e.requestOptions);

Upvotes: 3

Related Questions