我想用JSON做一个数组,但是我复制了相同的帖子. 我正在使用wordpress的睡觉API json插件.

有关WP REST API:https://v2.wp-api.org的更多信息

这是我的JSON

    [
  {
    "id": 65,
    "date": "2014-08-24T18:56:26",
    "date_gmt": "2014-08-24T18:56:26",
    "guid": {
      "rendered": "http:\/\/********\/********\/?p=1"
    },
    "modified": "2018-06-05T13:24:58",
    "modified_gmt": "2018-06-05T13:24:58",
    "slug": "this-url-wordpress",
    "status": "publish",
    "type": "post",
    "title": {
      "rendered": "\u2018 This a test title 1 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 1</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 1...<\/p>\n",
       "protected": false
     },
     "author": 1,
     "featured_media": 468,
     "comment_status": "open",
     "ping_status": "open",
     "sticky": false,
     "template": "",
     "format": "standard",
     "meta": [ 
     ],
     "categories": [
      14
     ],
     "tags": [
      17,
      18
     ],
  },
  {
    "id": 650,
    "date": "2014-08-24T18:56:26",
    "date_gmt": "2014-08-24T18:56:26",
    "guid": {
      "rendered": "http:\/\/********\/********\/?p=1"
    },
    "modified": "2018-06-05T13:24:58",
    "modified_gmt": "2018-06-05T13:24:58",
    "slug": "this-url-wordpress",
    "status": "publish",
    "type": "post",
    "title": {
      "rendered": "\u2018 This a test title 2 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 2</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 2...<\/p>\n",
       "protected": false
     },
     "author": 1,
     "featured_media": 468,
     "comment_status": "open",
     "ping_status": "open",
     "sticky": false,
     "template": "",
     "format": "standard",
     "meta": [ 
     ],
     "categories": [
      14
     ],
     "tags": [
      17,
      18
     ],
  },
  {
    "id": 230,
    "date": "2014-08-24T18:56:26",
    "date_gmt": "2014-08-24T18:56:26",
    "guid": {
      "rendered": "http:\/\/********\/********\/?p=1"
    },
    "modified": "2018-06-05T13:24:58",
    "modified_gmt": "2018-06-05T13:24:58",
    "slug": "this-url-wordpress",
    "status": "publish",
    "type": "post",
    "title": {
      "rendered": "\u2018 This a test title 3 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 3</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 3...<\/p>\n",
       "protected": false
     },
     "author": 1,
     "featured_media": 468,
     "comment_status": "open",
     "ping_status": "open",
     "sticky": false,
     "template": "",
     "format": "standard",
     "meta": [ 
     ],
     "categories": [
      14
     ],
     "tags": [
      17,
      18
     ],
  },
]

My code:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:peluqueriafran/WebView.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;

Future<Post> fetchPost() async {
  final response =
  await http.get('http://**********:88/WordPress/wp-json/wp/v2/posts/');
  final responseJson = json.decode(response.body);

  return new Post.fromJson(responseJson);
}

class Post {
    final int id;
    final String title;
    final String body;
    final String urlimagen;
    final String linkWeb;

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

   factory Post.fromJson(Map<String, dynamic> json) {
      return new Post(
         title: json['title']['rendered'].toString(),
      );
   }
}

WEB:
enter image description here

App: - The last generated is selected, I would like to get all the post.
I hope someone can help me out, thanks
enter image description here

推荐答案

您可以更改fetchPost以返回帖子列表,如下所示:

Future<List<Post>> fetchPosts() async {
  http.Response response =
      await http.get('http://**********:88/WordPress/wp-json/wp/v2/posts/');
  List responseJson = json.decode(response.body);
  return responseJson.map((m) => new Post.fromJson(m)).toList();
}

然后你可以像这样利用这Future<List<Post>>美元:

@override
Widget build(BuildContext context) {
  return new FutureBuilder<List<Post>>(
    future: fetchPosts(),
    builder: (context, snapshot) {
      if (!snapshot.hasData) return Container();
      List<Post> posts = snapshot.data;
      return new ListView(
        children: posts.map((post) => Text(post.title)).toList(),
      );
    },
  );
}

ListView接受子列表,就像Column一样,所以您可以使用任何包含子列表的小部件

Dart相关问答推荐

S,返回Future.sync()的函数和不需要等待的异步函数有什么区别?

`异步内联方法`的这些定义之间有什么区别?

angular.dart NgTwoWay 和 NgAttr 已弃用?

无法在 IOS 上使用 firebase 运行Flutter应用程序

Flutter.io (dart) - 如何创建左填充覆盖?

如何在 Flutter 包/插件开发中添加assets?

多级异步代码中的 Dart 错误

FInal和top-level lazy初始化

Flutter web url 导航

有没有办法知道我的Flatter应用程序使用的是哪个Android API版本?

如何在Dart中按布尔对列表进行排序

将JSON映射到类对象

在 Dart 中发送 SMTP 邮箱

轻松判断数字是否在 Dart 的给定范围内?

Dart如何将逗号添加到字符串编号

Dart vs Polymer vs Bootstrap

如何将 `ByteData` 实例写入 Dart 中的文件?

dart中是否可以有一个私有构造函数?

Dart:创建一个从 0 到 N 的列表

将方法或值添加到 dart 中的枚举