借助组件 dio
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:dio/dio.dart';
void main() => runApp(MyApp());
class MyAppextends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title:'Flutter Demo',
theme:ThemeData(
// This is the theme of your application.
//
// Try running your application with "flutter run". You'll see the
// application has a blue toolbar. Then, without quitting the app, try
// changing the primarySwatch below to Colors.green and then invoke
// "hot reload" (press "r" in the console where you ran "flutter run",
// or simply save your changes to "hot reload" in a Flutter IDE).
// Notice that the counter didn't reset back to zero; the application
// is not restarted.
primarySwatch: Colors.blue,
// This makes the visual density adapt to the platform that you run
// the app on. For desktop platforms, the controls will be smaller and
// closer together (more dense) than on mobile platforms.
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home:MyP(),
);
}
}
class MyPextends StatefulWidget {
@override
_MyPState createState() =>_MyPState();
}
class _MyPStateextends State {
String_data ="";
@override
Widget build(BuildContext context) {
return Container(
child:Column(
children: [
FlatButton(
onPressed: ()async {
print(1111);
Dio dio =new Dio();
Response res =await dio.get('https://www.baidu.com',queryParameters: {"username":"zhansan","psd":"123"});
dio.post('https://www.baidu.com',data: {"username":"zhansan","psd":"123"});
// 下载文件 下载地址 保存地址
// dio.download("https://www.baidu.com/logo.png", savePath)
print(res);
setState(() {
print(res.data.toString());
_data = res.data.toString();
});
},
child:Text("发起请求"),
color: Colors.red,
),
Scrollbar(
child:Container(
height:400,
child:SingleChildScrollView(
child:Text(_data),
),
))
],
),
);
}
}
网友评论