HttpResponse<T>.fromJson constructor

HttpResponse<T>.fromJson(
  1. Map<String, dynamic> json, {
  2. T fromJsonT(
    1. dynamic json
    )?,
})

Implementation

factory HttpResponse.fromJson(Map<String, dynamic> json,
    {T Function(dynamic json)? fromJsonT}) {
  final rawCode = json['code'];
  final code = switch (rawCode) {
    int value => value,
    num value => value.toInt(),
    String value => int.tryParse(value.trim()),
    _ => null,
  };
  if (code == null) {
    throw FormatException('Invalid response code: $rawCode');
  }
  return HttpResponse(
    code: code,
    message: json['message'] ?? json['msg'],
    data: json['data'] != null
        ? fromJsonT?.call(json['data']) ?? json['data']
        : null,
  );
}