一、配置pubspec.yaml文件

  1. //pubspec.yaml
  2. dependencies:
  3. dio: 2.1.16

二、先导入、后请求

  1. import 'package:flutter/material.dart';
  2. import 'package:dio/dio.dart';
  3. class _MyHomeState extends State {
  4. @override
  5. String msg;
  6. Widget build(BuildContext context) {
  7. return Scaffold(
  8. appBar: AppBar(
  9. title: Text('首页'),
  10. ),
  11. body: Row(
  12. mainAxisAlignment: MainAxisAlignment.start,
  13. children: <Widget>[
  14. FlatButton(
  15. child: Text("发送http"),
  16. onPressed: getHttp,
  17. ),
  18. Text("$msg")
  19. ],
  20. ),
  21. );
  22. }
  23. void getHttp() async {
  24. try {
  25. Response response = await Dio().get("https://douban.uieee.com/v2/movie/in_theaters");
  26. var title = response.data["title"];
  27. setState(() {
  28. msg = title;
  29. });
  30. } catch (e) {
  31. print(e);
  32. }
  33. }
  34. }