You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
818 B

2 years ago
import 'package:dio/dio.dart';
import 'package:json_annotation/json_annotation.dart';
part 'base_model.g.dart';
@JsonSerializable()
class BaseModel<T> {
int code;
String msg;
dynamic data;
2 years ago
2 years ago
BaseModel({
required this.code,
required this.msg,
this.data,
});
List<T> map(T Function(dynamic json) f) {
if (data == null) return [];
return (data! as List<T>).map(f).toList();
}
factory BaseModel.unknown() => BaseModel(
2 years ago
code: -1,
msg: 'UNKNOW FAIL',
);
2 years ago
factory BaseModel.dioErr(DioError err) => BaseModel(
2 years ago
code: err.response?.statusCode ?? -1,
msg: err.message,
);
2 years ago
factory BaseModel.fromJson(Map<String, dynamic> json) =>
_$BaseModelFromJson(json) as BaseModel<T>;
2 years ago
Map<String, dynamic> toJson() => _$BaseModelToJson(this);
2 years ago
}