目录:
- DioManager:Dio辅助类
- NWMethod:请求方法,get、post等
- NWApi:大家都知道
- EntityFactory:json转换辅助工厂,把json转为T
- BaseEntity:数据基类,返回的参数为 {“code”: 0, “message”: “”, “data”: {}}
- BaseListEntity:数据基类2,返回的参数为 {“code”: 0, “message”: “”, “data”: []}
- ErrorEntity:请求报错基类,{“code”: 0, “message”: “”}
EntityFactory:
class EntityFactory {
static T generateOBJ<T>(json) {
if (json == null) {
return null;
}
//可以在这里加入任何需要并且可以转换的类型,例如下面
// else if (T.toString() == "LoginEntity") {
// return LoginEntity.fromJson(json) as T;
// }
else {
return json as T;
}
}
}
BaseEntity:
class BaseEntity<T> {
int code;
String message;
T data;
BaseEntity({this.code, this.message, this.data});
factory BaseEntity.fromJson(json) {
return BaseEntity(
code: json["code"],
message: json["msg"],
// data值需要经过工厂转换为我们传进来的类型
data: EntityFactory.generateOBJ<T>(json["data"]),
);
}
}
BaseListEntity:
class BaseListEntity<T> {
int code;
String message;
List<T> data;
BaseListEntity({this.code, this.message, this.data});
factory BaseListEntity.fromJson(json) {
List<T> mData = new List<T>();
if (json['data'] != null) {
//遍历data并转换为我们传进来的类型
(json['data'] as List).forEach((v) {
mData.add(EntityFactory.generateOBJ<T>(v));
});
}
return BaseListEntity(
code: json["code"],
message: json["msg"],
data: mData,
);
}
}
ErrorEntity:
class ErrorEntity {
int code;
String message;
ErrorEntity({this.code, this.message});
}
NWApi:
class NWApi {
static final baseApi = "https://easy-mock.bookset.io/mock/5dfae67d4946c20a50841fa7/example/";
static final loginPath = "user/login";//接口返回:{"code": int, "message": "String", "data": {"account": "String", "password": "String"}}
static final queryListPath = "/query/list";//接口返回:{"code": ing, "message": "String", "data": [int, int, String, int, String, int]}
static final queryListJsonPath = "/query/listjson";//接口返回:{"code": int, "message": "String", "data": [{"account": "String", "password": "String"}, {"acc