itwo_flutter_net

Last updated:

0 purchases

itwo_flutter_net Image
itwo_flutter_net Images
Add to Cart

Description:

itwo flutter net

itwo_net #
一、引用
dependencies:
itwo_net:
git:
url: https://gitee.com/f_flutter/flutter_packages.git
path: itwo_net
ref: master
copied to clipboard
二、初始化
void main() {
HTTPManager.getInstance().init(BaseOptions(baseUrl: "https://url.url/"), interceptors: [LogInterceptor(responseBody: true)]);
runApp(MyApp());
}
copied to clipboard
三、网络请求
// Bo:网络请求统一管理
class Bo {
static Future<User> login(String password, {CancelToken cancelToken}) async {
Map<String, dynamic> map = {"password": password};
var response = await HTTPManager.getInstance().post("adm/login", cancelToken: cancelToken, data: new FormData.fromMap(map));
return response.take<User>();
}
}

//具体业务
var user = await Bo.login(_password);
//直接获取到 bean,异常会在 take 中统一处理掉
var id = user.id;
copied to clipboard
四、数据解析例子

extension Take<R> on Response {
HttpResponse<R> toHttpResponse<R>() {
Map<String, dynamic> map = this.data;
HttpResponse<R> httpResponse = HttpResponse<R>();
httpResponse.retCode = map["retCode"] ?? -1;
httpResponse.errorMsg = map["errorMsg"];
if (httpResponse.iSuccess()) {
httpResponse.data = JsonConvert.fromJsonAsT<R>(map["data"]);
}
return httpResponse;
}

HttpResponse<PageData<R>> toPageResponse<R>() {
Map<String, dynamic> map = this.data;
HttpResponse<PageData<R>> httpResponse = HttpResponse<PageData<R>>();
httpResponse.retCode = map["retCode"] ?? -1;
httpResponse.errorMsg = map["errorMsg"];
if (!httpResponse.iSuccess()) return httpResponse;

var pageMap = map["data"];
PageData<R> pageData = PageData<R>();
pageData.total = pageMap["total"];
pageData.curPage = pageMap["curPage"];
pageData.offset = pageMap["offset"];
pageData.over = pageMap["over"];
pageData.pageCount = pageMap["pageCount"];
pageData.size = pageMap["size"];
R.toString();
pageData.datas = JsonConvert.fromJsonAsT<List<R>>(pageMap["data"]);
httpResponse.data = pageData;
return httpResponse;
}

R take<R>({bool errorShow = true}) {
HttpResponse<R> httpResponse = toHttpResponse<R>();
if (!httpResponse.iSuccess()) {
if (errorShow && httpResponse.errorMsg != null) {
Toast.show(httpResponse.errorMsg);
}
throw new Exception(httpResponse.errorMsg);
}
return httpResponse.data;
}

PageData<R> takePage<R>({bool errorShow = true}) {
HttpResponse<PageData<R>> httpResponse = toPageResponse<R>();
if (!httpResponse.iSuccess()) {
if (errorShow && httpResponse.errorMsg != null) {
Toast.show(httpResponse.errorMsg);
}
throw new Exception(httpResponse.errorMsg);
}
return httpResponse.data;
}
}

class HttpResponse<T> extends IResponse {
int retCode;
T data;
String errorMsg;

@override
int iCode() => retCode;

@override
String iMessage() => errorMsg;

@override
bool iSuccess() => retCode == 0;
}

class PageData<T> {
List<T> datas;
}
copied to clipboard
依赖的第三方插件
# http https://pub.dev/packages/dio
dio: ^4.0.0
copied to clipboard

License:

For personal and professional use. You cannot resell or redistribute these repositories in their original state.

Files In This Product:

Customer Reviews

There are no reviews.