getObject method

Future<T> getObject({
  1. bool usingCache = false,
})

Implementation

Future<T> getObject({bool usingCache = false}) async {
  Map<String, dynamic> response;
  if (!usingCache) {
    if (method.body.isNotEmpty) {
      response = await method.request() as Map<String, dynamic>;
    } else {
      response = await method.requestJson() as Map<String, dynamic>;
    }
  } else {
    String? cachedResponse =
        await CacheResponseManager().getCachedResponseText(method);
    if (cachedResponse == null) {
      throw errorModel(cachedResponse, "NO-CACHED", ExpectType.list);
    }
    response = jsonDecode(cachedResponse) as Map<String, dynamic>;
  }
  if (response["data"] is! Map) {
    throw errorModel(response, "data is not compatible with expected data",
        ExpectType.object);
  }
  try {
    T result = fromMap(response["data"] as Map<String, dynamic>);
    if (T is ModelValidation) {
      String? validateError = (result as ModelValidation).validate();
      if (validateError != null) {
        throw errorModel(response, validateError, ExpectType.object);
      }
    }
    return result;
  } catch (e) {
    throw errorModel(response, e.toString(), ExpectType.object);
  }
}