Gülsen Keskin
Gülsen Keskin

Reputation: 800

How to access message in DioError

How can I access the "Message" field inside the e enter image description here

My postRequest function:

  Future<Response> postRequest(String endPoint, dynamic data) async {
    Response response;

    try {
      if (data != null) {
        response = await _dio!.post(endPoint, data: data);
      } else {
        response = await _dio!.post(endPoint);
      }
    } catch (e) {
      //I can reach the message from here and return it, but this 
      is not what I want to do.

      if (e is DioError) print(e.response);
      throw Exception(e);
    }

    return response;
  }

where I want to reach the message:

      var response;
      try{
        response = await http.postRequest("Stok/$type", requestObj);
      }catch(e){
        print("message....: $e");
      }

Upvotes: 0

Views: 208

Answers (1)

Safwan Mamji
Safwan Mamji

Reputation: 126

Future<Response> postRequest(String endPoint, dynamic data) async {
Response response;

try {
  if (data != null) {
    response = await _dio!.post(endPoint, data: data);
  } else {
    response = await _dio!.post(endPoint);
  }
  return response;
} on DioError {
  rethrow; // or throw e;
}
}

Now, just call it like that

 var response;
  try{
    response = await http.postRequest("Stok/$type", requestObj);
  } on DioError catch(e){
    print("message....: ${e.message}");
  }

Upvotes: 1

Related Questions