flutter网络请求的简单封装
- 存储token
- 单例模式
- 错误拦截
- 日志打印
- token拦截
shared_preferences: ^0.5.3+2
connectivity: ^0.4.3+2
dio: ^2.1.3
login(String username, String passWord) async {
HashMap<String, String> params = new HashMap();
params["userName"] = "短狐";
params["password"] ="1234567";
var res = await httpManager.post(Domain.login, params,null , new Options());
}
login(String username, String passWord) async {
HashMap<String, String> params = new HashMap();
params["userName"] = "短狐";
params["password"] ="1234567";
var res = await httpManager.get(Domain.login, params,null , new Options());
}
class NetConfig {
/// /// ////////////////////////////// DIO 配置 ////////////////////////////////////// ///
static const DEBUG = true;
static const int CONNECT_Timeout = 15000;
static const int RECEIVE_Timeout = 15000;
/// [RequestOptions] BASE_URL 如果 BASE_URL 就添加
static const String BASE_URL = "YOUR BASE_URL";
/// ///////////////////////////////// 常量 ////////////////////////////////// ///
static const TOKEN_KEY = "token";
static const USER_NAME_KEY = "user-name";
static const PW_KEY = "user-pw";
}
DEBUG=flase 时不显示打印日志
static const String BASE_URL = "YOUR BASE_URL" 为null的时候不配置
static const int CONNECT_Timeout = 15000; CONNECT_Timeout 时间
static const int RECEIVE_Timeout = 15000; RECEIVE_Timeout 时间