dart - Flutter:如何使用 JSON 数据创建数组

我想用 JSON 制作一个数组,但我复制了同一篇文章。 我正在为 WordPress 使用 REST 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
     ],
  },
]

我的代码:

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(),
      );
   }
}

网站:

App: - 最后生成的被选中,我要全部发帖。
我希望有人可以帮助我,谢谢

最佳答案

您可以更改 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等等,因此您可以使用任何包含子列表的小部件

https://stackoverflow.com/questions/50706500/

相关文章:

flutter - 带有 Scaffold 的 InheritedWidget 似乎无法正常工作

dart - 有没有人设法从谷歌登录 (Flutter) 获取 id token

android - Flutter 中的 MVVM 设计模式

dart - 使用标准库将字符串转换为 Dart 中的 GET 请求参数

android-camera - Flutter 相机叠加层

ios - Flutter 插件 `geolocator-Swift.h` 文件未找到

android - Flutter:使用 Kotlin 创建 Flutter 项目

dart - 如何将背景图像添加到 flutter 应用程序?

flutter - 无法访问 flutter.dev (Mac OS)

android - Flutter - 使用正则表达式验证电话号码