一、配置pubspec.yaml文件
//pubspec.yaml
dependencies:
dio: 2.1.16
二、先导入、后请求
import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
class _MyHomeState extends State {
@override
String msg;
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('首页'),
),
body: Row(
mainAxisAlignment: MainAxisAlignment.start,
children: <Widget>[
FlatButton(
child: Text("发送http"),
onPressed: getHttp,
),
Text("$msg")
],
),
);
}
void getHttp() async {
try {
Response response = await Dio().get("https://douban.uieee.com/v2/movie/in_theaters");
var title = response.data["title"];
setState(() {
msg = title;
});
} catch (e) {
print(e);
}
}
}