網(wǎng)絡(luò)請(qǐng)求
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("簡(jiǎn)單網(wǎng)絡(luò)請(qǐng)求")),
body: Container(
child: Column(
children: <Widget>[
TextField(
controller: controller,
decoration: InputDecoration(
contentPadding: EdgeInsets.all(10.0),
labelText: "名稱",
helperText: "請(qǐng)輸入要查詢的名稱"),
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('關(guān)鍵字為空'),
));
return '關(guān)鍵字為空';
} 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();
}
}
}
}