add netutil

hmxc
小赖 4 years ago
parent 0655d1fd20
commit 8a46f8f6e9

@ -0,0 +1,9 @@
class API {
static const String host = 'http://192.168.31.129:9001/app';
static const int networkTimeOut = 10000;
static _Login login = _Login();
}
class _Login {
String get a => '';
}

@ -1,5 +1,6 @@
import 'package:akuCommunity/pages/tab_navigator.dart'; import 'package:akuCommunity/pages/tab_navigator.dart';
import 'package:akuCommunity/provider/user_provider.dart'; import 'package:akuCommunity/provider/user_provider.dart';
import 'package:akuCommunity/utils/developer_util.dart';
import 'package:amap_map_fluttify/amap_map_fluttify.dart'; import 'package:amap_map_fluttify/amap_map_fluttify.dart';
import 'package:ani_route/ani_route.dart'; import 'package:ani_route/ani_route.dart';
import 'package:bot_toast/bot_toast.dart'; import 'package:bot_toast/bot_toast.dart';
@ -18,6 +19,7 @@ void main() {
WidgetsFlutterBinding.ensureInitialized(); WidgetsFlutterBinding.ensureInitialized();
ARoute.init(true); ARoute.init(true);
AmapLocation.instance.init(iosKey: 'ios key'); AmapLocation.instance.init(iosKey: 'ios key');
DeveloperUtil.setDev(true);
runApp(MyApp()); runApp(MyApp());
} }

@ -1,5 +1,7 @@
import 'package:akuCommunity/pages/sign/sign_in_page.dart'; import 'package:akuCommunity/pages/sign/sign_in_page.dart';
import 'package:akuCommunity/provider/user_provider.dart'; import 'package:akuCommunity/provider/user_provider.dart';
import 'package:akuCommunity/service/net.dart';
import 'package:akuCommunity/utils/net_util.dart';
import 'package:ani_route/ani_route.dart'; import 'package:ani_route/ani_route.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/cupertino.dart'; import 'package:flutter/cupertino.dart';
@ -23,13 +25,11 @@ class PersonalIndex extends StatefulWidget {
} }
class _PersonalIndexState extends State<PersonalIndex> class _PersonalIndexState extends State<PersonalIndex>
with AutomaticKeepAliveClientMixin, SingleTickerProviderStateMixin { with SingleTickerProviderStateMixin {
@override
bool get wantKeepAlive => true;
@override @override
void initState() { void initState() {
super.initState(); super.initState();
NetUtil().get('test');
} }
SliverAppBar _sliverAppBar(double height) { SliverAppBar _sliverAppBar(double height) {
@ -171,7 +171,6 @@ class _PersonalIndexState extends State<PersonalIndex>
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
super.build(context);
double _statusHeight = MediaQuery.of(context).padding.top; double _statusHeight = MediaQuery.of(context).padding.top;
return Scaffold( return Scaffold(
body: CustomScrollView( body: CustomScrollView(

@ -14,4 +14,4 @@ class RouterInit {
); );
}); });
} }
} }

@ -36,6 +36,7 @@ class Net {
(client) { (client) {
client.badCertificateCallback = client.badCertificateCallback =
(X509Certificate cert, String host, int port) { (X509Certificate cert, String host, int port) {
//TODO
return true; return true;
}; };
}; };

@ -0,0 +1,8 @@
class DeveloperUtil {
static bool _dev = false;
static setDev(bool state) {
_dev = state;
}
static bool get dev => _dev;
}

@ -0,0 +1,84 @@
import 'dart:convert';
import 'package:bot_toast/bot_toast.dart';
import 'package:dio/dio.dart';
import 'package:akuCommunity/constants/api.dart';
class NetUtil {
Dio _dio;
static final NetUtil _netUtil = NetUtil._internal();
factory NetUtil() => _netUtil;
NetUtil._internal() {
BaseOptions options = BaseOptions(
baseUrl: API.host,
connectTimeout: API.networkTimeOut,
receiveTimeout: API.networkTimeOut,
sendTimeout: API.networkTimeOut,
headers: {},
);
if (_dio == null) _dio = Dio(options);
}
Future<BaseModel> get(
String path, {
Map<String, dynamic> params,
}) async {
try {
Response res = await _dio.get(path, queryParameters: params);
return BaseModel.fromJson(res.data);
} on DioError catch (e) {
_parseErr(e);
}
return BaseModel.err();
}
_parseErr(DioError err) {
_makeToast(String message) {
BotToast.showText(text: '$message\_${err.response.statusCode}');
}
switch (err.type) {
case DioErrorType.CONNECT_TIMEOUT:
case DioErrorType.SEND_TIMEOUT:
case DioErrorType.RECEIVE_TIMEOUT:
_makeToast('连接超时');
break;
case DioErrorType.RESPONSE:
_makeToast('服务器出错');
break;
case DioErrorType.CANCEL:
break;
case DioErrorType.DEFAULT:
_makeToast('未知错误');
break;
}
}
}
class BaseModel {
int code;
String message;
dynamic data;
BaseModel({
this.code,
this.message,
this.data,
});
BaseModel.err({this.message = '未知错误', this.code = 0});
factory BaseModel.fromMap(Map<String, dynamic> map) {
if (map == null) return null;
return BaseModel(
code: map['code'] ?? 0,
message: map['message'] ?? '',
data: map['data'],
);
}
factory BaseModel.fromJson(String source) =>
BaseModel.fromMap(json.decode(source));
}
Loading…
Cancel
Save