Flutter Dio二次封装

return json as T;
}
}
}

BaseEntity:

class BaseEntity {
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(json[“data”]),
);
}
}

BaseListEntity:

class BaseListEntity {
int code;
String message;
List data;

BaseListEntity({this.code, this.message, this.data});

factory BaseListEntity.fromJson(json) {
List mData = List();
if (json[‘data’] != null) {
//遍历data并转换为我们传进来的类型
(json[‘data’] as List).forEach((v) {
mData.add(EntityFactory.generateOBJ(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”}, {“account”: “String”, “password”: “String”}]}
}

NWMethod:

enum NWMethod {
GET,
POST,
DELETE,
PUT
}
//使用:NWMethodValues[NWMethod.POST]
const NWMethodValues = {
NWMethod.GET: “get”,
NWMethod.POST: “post”,
NWMethod.DELETE: “delete”,
NWMethod.PUT: “put”
};

下面就可以进行正式的封装了:

第一步是创建Dio的单例辅助类,并初始化Dio和对Dio进行一些全局的参数设置:

import ‘package:dio/dio.dart’;
import ‘package:flutter_app/network/NWApi.dart’;
import ‘package:flutter_app/utils/PrintUtil.dart’;
class DioManager {
static final DioManager _shared = DioManager._internal();
factory DioManager() => _share

  • 30
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值