bodyDecoded<T> function Null safety

T? bodyDecoded<T>(
  1. Request<T> request,
  2. String stringBody,
  3. String? mimeType
)

Implementation

T? bodyDecoded<T>(Request<T> request, String stringBody, String? mimeType) {
  T? body;
  var bodyToDecode;

  if (mimeType != null && mimeType.contains('application/json')) {
    try {
      bodyToDecode = jsonDecode(stringBody);
    } on FormatException catch (_) {
      Get.log('Cannot decode server response to json');
      bodyToDecode = stringBody;
    }
  } else {
    bodyToDecode = stringBody;
  }

  try {
    if (stringBody == '') {
      body = null;
    } else if (request.decoder == null) {
      body = bodyToDecode as T?;
    } else {
      body = request.decoder!(bodyToDecode);
    }
  } on Exception catch (_) {
    body = stringBody as T;
  }

  return body;
}