欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

demo02 使用flutter发起http请求

程序员文章站 2022-05-29 12:09:20
...

首先要在pubspec.yaml里添加以下三方库

然后dependencies 下面的flutter里加上

dependencies:

  flutter:

  sdk: flutter

  http: ^0.12.0+1

最后在命令行里输入flutter packages get 安装支持库

 

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

void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new MaterialApp(
      title: 'http请求示例',
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text('http请求示例'),
        ),
        body: new Center(
          child: new RaisedButton(
              onPressed: (){
                var url = 'http://httpbin.org/';
                //向'http://httpbin.org/'发送get请求
                http.get(url).then((response) {
                  print("状态: ${response.statusCode}");
                  print("正文: ${response.body}");
                });
              },
            child: new Text('发起http请求'),
          ),
        ),
      ),
    );
  }
}