
网络请求
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
class HomePage extends StatefulWidget {
@override
State<StatefulWidget> createState() {
return HomePageState();
}
}
class HomePageState extends State<HomePage> {
String result = '';
TextEditingController controller = TextEditingController();
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text("简单网络请求")),
body: Container(
child: Column(
children: <Widget>[
TextField(
controller: controller,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(10.0),
labelText: "名称",
helperText: "请输入要查询的名称"),
autofocus: false,
),
RaisedButton(
onPressed: () {
var params = controller.text.trim().toString();
Future future = getUser(params);
future.then((value) {
print('value---------$value');
setState(() {
// result = value['data']['name'].toString();
result = value.toString();
});
});
},
child: Text('查询'),
),
Text(result)
],
),
),
);
}
Future getUser(String name) async {
if (name == '') {
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text('提示'),
content: Text('关键字为空'),
));
return '关键字为空';
} else {
try {
print('params---------$name');
var param = {'userName': name};
var response = await Dio().get(
"http://localhost:11220/api/userInfo/getUserListAll",
queryParameters: param);
return response.data;
} catch (e) {
return e.toString();
}
}
}
}
网友评论