toResult<T> static method

T toResult<T>(
  1. List<int> bytes
)

Converts a list of bytes into a result of type T.

Handles various types of conversions, including JSON decoding, byte lists, and casting to other types.

  • bytes: The response body as a list of bytes.

Returns: The parsed response as an object of type T.

Implementation

static T toResult<T>(List<int> bytes) {
  if (bytes.isEmpty && null is T) {
    return null as T;
  }
  if (dynamic is T) {
    return StringUtils.toJson(StringUtils.decode(bytes));
  }
  if (<dynamic>[] is T) {
    return StringUtils.toJson(StringUtils.decode(bytes));
  }
  if (<int>[] is T) {
    return bytes as T;
  }
  final resultString = StringUtils.decode(bytes);
  if (<String>[] is T) {
    return StringUtils.toJson<List>(resultString).cast<String>() as T;
  }
  if (<bool>[] is T) {
    return StringUtils.toJson<List>(resultString).cast<bool>() as T;
  }
  if (0 is T) {
    return IntUtils.parse(resultString) as T;
  }
  if (BigInt.zero is T) {
    return BigintUtils.parse(resultString) as T;
  }
  if (<String, dynamic>{} is T) {
    return StringUtils.toJson(resultString);
  }
  if (<Map<String, dynamic>>[] is T) {
    return StringUtils.toJson<List>(resultString)
        .map((e) => (e as Map).cast<String, dynamic>())
        .toList() as T;
  }
  return resultString as T;
}