Flutter:如何使用JSON加载所有分页数据

时间:2018-06-07 20:06:18

标签: dart flutter

我有一个 wordpress 页面,我发布帖子。 当我在Wordpress中超过10个出版物时发现问题,该应用程序仅收到最近10个出版物

我想实现显示所有帖子。但我不知道如何开始。

目前我有15个出版物,正如您在图片中看到的那样,只显示了最后10个,我希望能解决这个问题,谢谢

这是我的 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
     ],
  },
  {
    "id": 236,
    "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 4 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 4</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 4...<\/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": 330,
    "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 5 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 5</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 5...<\/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 6 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 6</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 6...<\/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 7 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 7</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 7...<\/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 8 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 8</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 8...<\/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 9 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 9</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 9...<\/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 10 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 10</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 10...<\/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 11 \u2019"
     },
     "content": {
       "rendered": "<p>This is a content 11</p>",
       "protected": false
     },
     "excerpt": {
       "rendered": "<p>this a excerpt 11...<\/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
     ],
  },
]

这是我的 Dart 代码:

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

class Post {
    final String title;
    final String body;
    final String content;
    final String urlimagen;
    final String linkWeb;

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

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

  class NewsState extends State<NewsPage> {
    int _count = 0;

    @override
    Widget build(BuildContext context) {
      return Scaffold(
        appBar: new AppBar(
          title: new Text("Noticias"),
          centerTitle: true,
          backgroundColor: Colors.deepOrange[700],
        ),
        body: new Container(
          child: new RefreshIndicator(
              child: new ListView(
                children: <Widget>[
                 new FutureBuilder<List<Post>>(
                      future: fetchPosts(),
                      builder: (context, snapshotImg) {
                        if (snapshotImg.hasData){
                          List<Post> posts = snapshotImg.data;
                          return new Column(
                          children: posts.map((post) => new Column(
                          children: _getItems(post.title, post.body, post.content, post.linkWeb),
                          )).toList()
                        );
                      }
                      else if(snapshotImg.hasError)
                      {
                        return new Text("${snapshotImg.error}");
                      }
                        return new Center(
                          child: new Column(
                             children: <Widget>[
                              new Padding(padding: new dgeInsets.all(50.0)),
                              new CircularProgressIndicator(),
                            ],
                          ),
                        );
                      },
                    ),
                  ],
                ),
              onRefresh: _handleRefresh,
          ),
        )
      );
    }

    List<Widget> _getItems(String title, String description, String content, String url) {
      var items = <Widget>[];
      var item = new Column(
          children: <Widget>[
            new ListTile(
              leading: new Icon(Icons.language, color: Colors.deepOrange[700], size: 30.0,),
              title: new Text(title),
              subtitle: new Text(description),
              onTap: ()
              {
                Navigator.push(
                    context,
                    new MaterialPageRoute(builder: (context) => new WebView(url: url, title: title)));
              },
            ),
                  new Divider(
                    color: Colors.deepOrange[700],
              height: 2.0,
            ),
          ],
        );
        items.add(item);
      return items;
    }

    Future<Null> _handleRefresh() async {
      await new Future.delayed(new Duration(seconds: 3));

      setState(() {
        _count += 5;
      });

      return null;
    }
}

应用内容的示例 enter image description here

1 个答案:

答案 0 :(得分:0)

我对此问题的解决方案是由于Wordpress JSON API REST V2插件,限制我使用10个发布

更多信息请参阅: Wordpress API JSON return limit

感谢您的帮助,谢谢。