本片博客进行基本网络的请求,
源码:
import 'package:flutter/material.dart';
import 'package:flutter_shop/index_page_test/index_page.dart';
///能套一个方便的组件的话,就套一个,方便以后修改
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
const MyApp({Key key}) : super(key: key);
@override
Widget build(BuildContext context) {
return MaterialApp(
home: IndexPage(),
);
}
}
///实现简单从网络获取数据并在页面进行显示
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
class IndexPage extends StatefulWidget {
IndexPage({Key key}) : super(key: key);
_IndexPageState createState() => _IndexPageState();
}
class _IndexPageState extends State<IndexPage> {
TextEditingController questionController;
String responseText;
@override
void initState() {
super.initState();
questionController = TextEditingController();
responseText = '';
}
@override
void dispose() {
questionController.dispose();
super.dispose();
}
_showDialog() async{
if(questionController.text.toString() != null){
getHttp().then((val){
setState(() {
responseText = val;
});
});
}else{
showDialog(
context: context,
builder: (val)=> AlertDialog(title: Text('内容不能为空'),),
);
}
}
Future<dynamic> getHttp() async{
Response response;
response = await Dio().get('http://www.baidu.com',
queryParameters: {
'jik':'nkj',
});
return response.data;
}
@override
Widget build(BuildContext context) {
Widget body = Container(
padding: EdgeInsets.symmetric(vertical: 10),
child: Column(
children: <Widget>[
TextField(
controller: questionController,
autofocus: false,
decoration: InputDecoration(
labelText: '问题',
helperText: '请输入您的要搜索的内容',
),
),
RaisedButton(
child: Text('搜索'),
onPressed: _showDialog,
),
Text(
responseText,
textAlign: TextAlign.center,
maxLines: 10,
overflow: TextOverflow.ellipsis,
)
],
),
);
return Scaffold(
appBar: AppBar(title: Text('我的“搜索”'),),
body: body,
);
}
}
运用 Dio 开发,使用 Get 方法,Future 返回值
也可以使用 Post 方法