ApiResponse.dynamic constructor

ApiResponse.dynamic(
  1. dynamic body, {
  2. TransferBean? bean,
  3. int statusCode = 200,
})

Automatically creates the corresponding ApiResponse implementation for body.

Allowed types for body are: null, ApiResponse, Uint8List, ByteData, File, Map<String, dynamic>, List

Implementation

factory ApiResponse.dynamic(dynamic body,
    {TransferBean? bean, int statusCode = 200}) {
  if (body == null) {
    return EmptyResponse(statusCode: statusCode);
  } else if (body is ApiResponse) {
    return body;
  } else if (body is Uint8List) {
    return RawResponse(body);
  } else if (body is ByteData) {
    return RawResponse(body.buffer.asUint8List());
  } else if (body is io.File) {
    return FileResponse(body);
  } else if (body is Map<String, dynamic> ||
      body is List<dynamic> ||
      body is TransferObjectBase) {
    return JsonResponse(body, statusCode);
  } else if (body is Stream<List<int>>) {
    throw ApiError(
        'A data stream cannot be used as response type without a length argument.'
        'Use ByteStreamResponse or FileResponse as return type instead to provide the length.');
  } else {
    return TextResponse.plain(body.toString(), statusCode: statusCode);
  }
}