feat: api result add code (#96)

This commit is contained in:
Li Wei authored and GitHub committed 2023-03-05 11:54:39 +08:00
1 parent dbbbc204fb
commit 2e639e0fef
9 files changed
+130 -89

No files matched your search

+7 -5
View File
@@ -70,15 +70,17 @@ Future<T> _parse<T>(
try {
var resp = await fetch();
if (fromJsonT != null) {
return Result<T>.fromJson(resp.data, fromJsonT).data as T;
final result = Result<T>.fromJson(resp.data, fromJsonT);
if (result.code == 0) {
return result.data as T;
} else {
throw Exception(result);
}
} else {
return null as T;
}
} on DioError catch (e) {
if (e.response == null) {
throw Exception("Server error");
}
throw Exception(Result.fromJson(e.response?.data, (_) => null).msg);
throw Exception(Result(code: 1000, msg: e.message));
}
}
+3
View File
@@ -4,10 +4,12 @@ part 'result.g.dart';
@JsonSerializable(genericArgumentFactories: true)
class Result<T> {
int code;
String? msg;
T? data;
Result({
required this.code,
this.msg,
this.data,
});
@@ -18,6 +20,7 @@ class Result<T> {
) =>
_$ResultFromJson(json, fromJsonT);
Map<String, dynamic> toJson() => {
'code': code,
'msg': msg,
'data': data is List
? (data as dynamic)?.map((e) => e.toJson()).toList()
+4 -1
View File
@@ -11,6 +11,7 @@ Result<T> _$ResultFromJson<T>(
T Function(Object? json) fromJsonT,
) =>
Result<T>(
code: json['code'] as int,
msg: json['msg'] as String?,
data: _$nullableGenericFromJson(json['data'], fromJsonT),
);
@@ -19,7 +20,9 @@ Map<String, dynamic> _$ResultToJson<T>(
Result<T> instance,
Object? Function(T value) toJsonT,
) {
final val = <String, dynamic>{};
final val = <String, dynamic>{
'code': instance.code,
};
void writeNotNull(String key, dynamic value) {
if (value != null) {