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.
|
|
|
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;
|
|
|
|
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(
|
|
|
|
code: -1,
|
|
|
|
msg: 'UNKNOW FAIL',
|
|
|
|
);
|
|
|
|
|
|
|
|
factory BaseModel.dioErr(DioError err) => BaseModel(
|
|
|
|
code: err.response?.statusCode ?? -1,
|
|
|
|
msg: err.message,
|
|
|
|
);
|
|
|
|
|
|
|
|
Map<String, dynamic> toJson() => _$BaseModelToJson(this);
|
|
|
|
|
|
|
|
factory BaseModel.fromJson(Map<String, dynamic> json) =>
|
|
|
|
_$BaseModelFromJson(json) as BaseModel<T>;
|
|
|
|
}
|