flutter http get请求

Posted sea-stream

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flutter http get请求相关的知识,希望对你有一定的参考价值。

 

import dart:async;
import dart:convert;

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

Future<Post> fetchPost() async {
  final response =
  await http.get(https://jsonplaceholder.typicode.com/posts/1);
  final responseJson = json.decode(response.body);

  return new Post.fromJson(responseJson);
}

class Post {
  final int userId;
  final int id;
  final String title;
  final String body;

  Post({this.userId, this.id, this.title, this.body});

  factory Post.fromJson(Map<String, dynamic> json) {
    return new Post(
      userId: json[userId],
      id: json[id],
      title: json[title],
      body: json[body],
    );
  }
}

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: Fetch Data Example,
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new Scaffold(
        appBar: new AppBar(
          title: new Text(Fetch Data Example),
        ),
        body: new Center(
          child: new FutureBuilder<Post>(
            future: fetchPost(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return new Text(snapshot.data.title);
              } else if (snapshot.hasError) {
                return new Text("${snapshot.error}");
              }

              // By default, show a loading spinner
              return new CircularProgressIndicator();
            },
          ),
        ),
      ),
    );
  }
}

 

 

 

 

 

以上是关于flutter http get请求的主要内容,如果未能解决你的问题,请参考以下文章

关于APIT定位算法的讨论

转:PHP中的使用curl发送请求(GET请求和POST请求)

Jersey REST GET 正在工作,但 PUT 没有。请求的资源不允许指定的 HTTP 方法

带有 file_get_contents 的 HTTP 请求,获取响应代码

请求的资源不支持 http 方法“GET”。错误代码 405

PHP常用代码片段