dio_get請求
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
class HomePage extends StatefulWidget {
HomePage({Key key}) : super(key: key);
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
TextEditingController typeController = TextEditingController();
String showText = '歡迎來到上海白馬高級會所';
@override
Widget build(BuildContext context) {
return Container(
child: Scaffold(
appBar: AppBar(title: Text('上海白馬會所'),),
body: Container(
child: Column(
children: <Widget>[
TextField(
// 監(jiān)聽文本字段的變化
controller: typeController,
decoration: InputDecoration(
labelText: '帥哥類型',
helperText: '請輸入你喜歡的類型'
),
// 關(guān)閉軟鍵盤自動彈起
autofocus: false,
),
RaisedButton(
onPressed: _choiceAction,
child: Text('點鴨'),
),
Text(
showText,
overflow: TextOverflow.ellipsis,
maxLines: 1,
)
],
),
),
)
);
}
_choiceAction(){
// 判斷文本框是否為空,如果為空就不查詢
if(typeController.text.toString() == ''){
showDialog(
context: context,
// 提示用戶為空
builder: (context) => AlertDialog(title: Text('文本框不能為空'))
);
}else{
// 拿到用戶輸入的文字售貌,并更新畫面
getHttp(typeController.text.toString()).then((val){
setState(() {
showText = val['data']['name'].toString();
});
});
}
}
Future getHttp(String TypeText) async {
try{
Response response;
var data = {'name':TypeText};
// 這個鏈接是easy-mock 里面做的假數(shù)據(jù)
// 如果使用post請求直接在這里換成post請求方式就行了
response = await Dio().get("https://www.easy-mock.com/mock/5c60131a4bed3a6342711498/baixing/dabaojian",
queryParameters:data
);
return response.data;
}catch(e){
print(e);
}
}
}