1.网络,导入Dio库,dio: 4.0.0
,注意要用空格,不能碰到边界。
2.dio网络请求:
//根据需要传入对应的参数
Dio().get(url, queryParameters: {"type": type, "key": key});
3.回调
typedef RequestCallBack = void Function(Map data);
//请求完成后根据得到的数据执行回调
static Future<Response> getData(RequestCallBack callBack) async{
String url = "https://v.juhe.cn/toutiao/index";
String key = "4c52313fc9247e5b4176aed5ddd56ad7";
String type = "keji";
print("开始请求数据");
var map = Map<String,String>();
map.putIfAbsent("111", () => "222");
map.putIfAbsent("222", () => "333");
Response response = await Dio().get(url, queryParameters: {"type": type, "key": key});
print("请求完成");
callBack(map);
return response;
}
//
HttpRequest.getData((Map map){map.forEach((key, value) {print("key:${key},value:${value}");});});
4.异步:async 、await ,await 的代码块会阻塞后面的代码执行,直到代码块返回需要的对象。Future可以return任意类型,可以看成泛型。
5.flutter中3种网络请求来源:学习来源
6.json解析,导入import 'dart:convert';
添加依赖:
assets:
- assets/images/
- mock/
在mock文件夹下放置待加载的资源xxxx.json,使用rootBundle加载模拟资源xxx.json
var responseStr = await rootBundle.loadString('mock/$action.json');
var responseJson = json.decode(responseStr);
responseStr类型为String,responseJson 类型为Map<String,dynamic>,
此时对responseJson 通过key取值,如果是数组[]则返回List<Map<String,dynamic>>,如果是{}则返回Map<String,dynamic>。
var subobject = value['subjects'];
var subObjectList = subobject.map<SubjectEntity>((item)=>SubjectEntity.fromMap(item)).toList();
SubjectEntity sub = subObjectList[0];
迭代器类里有map方法,作用是遍历修改集合内的元素,并返回修改后的元素,可以通过toList返回一个List.
7.私有变量是下划线开头,通过特定的get set修饰符进行取值或修改。
var _avatar;
dynamic get getAvatar{
return this._avatar;
}
set setAvatar(dynamic avatar){
this._avatar = _avatar;
}
dart没有范围控制符public、private等,下划线开头就是私有的,不是下划线开头就是公共的。
8.下划线除了有声明私有外,还表示我用不到此参数,我可以用_糊弄,不必传具体的参数,,比如调用某个有返回值的函数后的then方法会传一个value值,但是如果我们的函数体内没有用到此参数,可以使用_表示。
9.flutter返回键弹出退出对话框:界面的最外层一定是WillPopScope控件,onWillPop是返回键触发时的调用,showDialog是sdk固定调用弹窗的方法。
return new WillPopScope(
onWillPop: ()async =>showDialog(context: context, barrierDismissible: true,builder: (context){
return ExitDialog(title: '提示', content: '您确定要退出吗?',confirmText: '确定',cancelText: '取消',
cancelFun: (){Navigator.of(context).pop(false);},
confirmFun: (){Navigator.of(context).pop(true);});
},
),
child: new Scaffold(
backgroundColor: Colors.black,
body: new Center(
child: _currentPage,
),
drawer: Drawer(
child: ListView(
padding: EdgeInsets.zero,
children: const <Widget>[
DrawerHeader(
decoration: BoxDecoration(
color: Colors.blue,
),
child: Text(
'Drawer Header',
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
),
),
ListTile(
leading: Icon(Icons.message),
title: Text('Messages'),
),
ListTile(
leading: Icon(Icons.account_circle),
title: Text('Profile'),
),
ListTile(
leading: Icon(Icons.settings),
title: Text('Settings'),
),
],
),
),
bottomNavigationBar: bottomNavigationBar,
),
);
自定义的弹窗ExitDialog
class ExitDialog extends Dialog{
String title;
String content;
String cancelText;
String confirmText;
Function cancelFun;
Function confirmFun;
ExitDialog( {
Key key,@required this.title,@required this.content,@required this.cancelText,
@required this.confirmText,@required this.cancelFun,@required this.confirmFun}):super(key: key);
@override
Widget build(BuildContext context) {
return Padding(
padding: EdgeInsets.all(15),
child: Material(
type: MaterialType.transparency,
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Container(
decoration: ShapeDecoration(
color: Color(0xff020202),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(10),
),
),
),
margin: EdgeInsets.all(15),
child: Column(
children: [
Padding(
padding: EdgeInsets.all(10),
child: Center(
child: Text(title,style: TextStyle(color: Color(0xff666666)),),
),
),
Container(
color: Color(0xffffffff),
margin: EdgeInsets.only(left: 10,right: 10),
height: 1,
),
Container(
constraints: BoxConstraints(minHeight: 100),
child: Container(
padding: const EdgeInsets.all(12.0),
alignment: Alignment.center,
child: IntrinsicHeight(
child: Text(content,style: TextStyle(color: Color(0xff666666)),),
),
),
),
Container(
color: Color(0xffffffff),
height: 1,
),
_buildBottomGroup(),
],
),
)
],
),
),
);
}
Widget _buildBottomNegativeButton(){
return Flexible(fit:FlexFit.tight,child: FlatButton(
onPressed: this.cancelFun,
child: Text(cancelText,style: TextStyle(color: Color(0xff666666)),),
)
);
}
Widget _buildBottomPositiveButton(){
return Flexible(fit:FlexFit.tight,child: FlatButton(
onPressed: this.confirmFun,
child: Text(confirmText,style: TextStyle(color: Color(0xff666666)),),
)
);
}
Widget _buildBottomFence(){
return Container(
color: Color(0xffeeeeee),
height: 38,
width: 1,
);
}
Widget _buildBottomGroup(){
var widgets = <Widget>[];
if(cancelText!=null&&cancelText.isNotEmpty) widgets.add(_buildBottomNegativeButton());
if(confirmText!=null&&confirmText.isNotEmpty) widgets.add(_buildBottomFence());
if(confirmText!=null&&confirmText.isNotEmpty) widgets.add(_buildBottomPositiveButton());
return Flex(direction: Axis.horizontal,children: widgets);
}
}