flutter相關git
www.flutterj.com/nCoV-2019.a…程序員
IOS: 拉下代碼直接跑便可github
這段時間肺炎比較嚴重,你們記得戴口罩json
你們好,我又來蹭熱點了。微信
1.26日02:00看到了:markdown
從而有了這篇文章。架構
我不是什麼專業人士,也沒法貢獻醫療力量,只能盡我所能看是否能爲這件事作一點點的貢獻。app
還有就是始終沒法勸動父母戴口罩,我爸今天還打牌到凌晨2點左右纔回來,他去以前我都跟他約定好了,我不去網吧上網了,他也別去打牌了,並講述了此次疫情的重要性, 無果。框架
App內全部資訊來自人民日報和丁香醫生,官方實時動態更新,經驗證。async
本次App研發主要採用dart語言,flutter框架,MVVM架構設計,採用上文提供的接口進行開發,代碼很是簡潔可觀,註釋明確。
![]() |
![]() |
![]() |
![]() |
---|---|---|---|
![]() |
![]() |
![]() |
![]() |
封裝了請求Model和ViewModel;
View層暫無,因此還不算完整,待更新
// 請求計數
var _id = 0;
/* * 請求類型枚舉 * */
enum RequestType { GET, POST }
class ReqModel {
///鏈接超時時間爲5秒
static const int connectTimeOut = 5 * 1000;
///響應超時時間爲7秒
static const int receiveTimeOut = 7 * 1000;
// 請求url路徑
String url() => null;
// 請求參數
Map params() => {};
/* * get請求 * */
Future<dynamic> get() async {
return this._request(
url: url(),
method: RequestType.GET,
params: params(),
);
}
/* * post請求 * */
Future post() async {
return this._request(
url: url(),
method: RequestType.POST,
params: params(),
);
}
/* * post請求-文件上傳方式 * */
Future postUpload(
ProgressCallback progressCallBack, {
FormData formData,
}) async {
return this._request(
url: url(),
method: RequestType.POST,
formData: formData,
progressCallBack: progressCallBack,
params: params(),
);
}
/* * 請求方法 * */
Future _request({
String url,
RequestType method,
Map params,
FormData formData,
ProgressCallback progressCallBack,
}) async {
Dio _client;
final httpUrl = '$reqUrl$url';
if (_client == null) {
BaseOptions options = new BaseOptions();
options.connectTimeout = connectTimeOut;
options.receiveTimeout = receiveTimeOut;
options.headers = const {'Content-Type': 'application/json'};
options.baseUrl = reqUrl;
_client = new Dio(options);
}
final id = _id++;
int statusCode;
try {
Response response;
if (method == RequestType.GET) {
///組合GET請求的參數
if (mapNoEmpty(params)) {
response = await _client.get(
url,
queryParameters: params,
);
} else {
response = await _client.get(
url,
);
}
} else {
if (mapNoEmpty(params) && formData.isNotEmpty) {
response = await _client.post(
url,
data: formData ?? params,
onSendProgress: progressCallBack,
);
} else {
response = await _client.post(
url,
);
}
}
statusCode = response.statusCode;
if (response != null) {
print('HTTP_REQUEST_URL::[$id]::$httpUrl');
if (mapNoEmpty(params)) print('HTTP_REQUEST_BODY::[$id]::$params');
print('HTTP_RESPONSE_BODY::[$id]::${json.encode(response.data)}');
return response.data;
}
///處理錯誤部分
if (statusCode < 0) {
return _handError(statusCode);
}
} catch (e) {
return _handError(statusCode);
}
}
///處理異常
static Future _handError(int statusCode) {
String errorMsg = 'Network request error';
Map errorMap = {"errorMsg": errorMsg, "errorCode": statusCode};
print("HTTP_RESPONSE_ERROR::$errorMsg code:$statusCode");
return Future.value(errorMap);
}
}
複製代碼
帶參數例子:
來自普通程序員, 連接:juejin.im/post/5e2c6a…
項目在不斷更新,目前架構和請求等數據處理方式已封裝完畢。
正在進行View層和推送的開發。
接口做者(普通程序員)也在不段優化和更新,在此致敬!
若是你們有好的意見或者有好的設計圖的話能夠羣內找我。
Flutter交流QQ羣:874592746
Flutter交流微信羣: