HttpResponse<T>.fromJson constructor
HttpResponse<T>.fromJson(
- Map<String, dynamic> json, {
- T fromJsonT(
- 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,
);
}