微信交流群

http 是一个可组合,基于Future的库,用于HTTP请求。该软件包包含高级功能和类,可轻松使用HTTP资源。它是多平台的,并且支持移动设备,台式机和浏览器。此软件包为官方出品

pub 地址:https://pub.dev/packages/http

Github 地址:https://github.com/dart-lang/http

# 引入软件包

pubspec.yaml 中添加如下依赖:

dependencies:
  http: ^0.12.2
1
2

执行命令:

flutter pub get
1

# 使用

发起 get 请求:

import 'package:http/http.dart';

var http = Client();
Response response = await http.get('https://github.com/781238222/flutter-do/blob/master/README.md');
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
1
2
3
4
5
6

为了方便阅读,将包作为 http 调用:

import 'package:http/http.dart' as http;

http.Response response = await client.get('https://github.com/781238222/flutter-do/blob/master/README.md');
print('Response status: ${response.statusCode}');
print('Response body: ${response.body}');
1
2
3
4
5

发起 post 请求:

var url = 'https://example.com/whatsit/create';
var response = await http.post(url, body: {'name': 'doodle', 'color': 'blue'});
1
2

下面通过 http 获取 https://github.com/781238222/flutter-do/blob/master/README.md 内容:

import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

///
/// desc:
///

class HttpPluginDemo extends StatefulWidget {
  
  _HttpPluginDemoState createState() => _HttpPluginDemoState();
}

class _HttpPluginDemoState extends State<HttpPluginDemo> {
  String _data;

  getData() async {
    var client = http.Client();
    http.Response response = await client
        .get('https://github.com/781238222/flutter-do/blob/master/README.md');
    setState(() {
      _data = response.body;
    });
  }

  
  void initState() {
    super.initState();
    getData();
  }

  
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('HttpPluginDemo'),
      ),
      body: Center(
        child: Text('$_data'),
      ),
    );
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42