Flutter:从内容列表中获取呈现的文本 [英] Flutter: Get rendered text from content list
问题描述
我想获取我的WordPress页面的所有出版物,并且使用以下插件作为基础。
这是我的 JSON >由 WP REST API 插件生成的代码
[
{
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这是测试标题1 \u2019
},
内容:{
rendered:< p>这是内容1< ; / p>,
受保护:false
},
摘录:{
rendered:< p>这是摘录1 ... < \ / p> \n,
受保护:false
},
作者:1,
featured_media:468,
comment_status:开放,
ping_status:开放,
粘性:false,
模板:,
格式:标准,
元:[
],
类别:[
14
],
标签:[
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>这是内容2< / p>,
受保护:假
},
摘录:{
rendered:< p> this a摘录2 ...< \ / p&\; n,
protected :false
},
作者:1,
featured_media:468,
comment_status:打开,
ping_status:打开,
sticky:假,
template:,
format: standard,
meta:[
],
类别:[
14
],
标签:[
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
},
内容:{
已渲染:< p>这是内容3< / p>,
受保护:假
},
摘录:{{
rendered:< p> this a excerpt 3 ...< \ / p&\n,
protected:false
} ,
作者:1,
featured_media:468,
comment_status:开放,
ping_status:开放,
sticky:错误,
template:,
format: standard,
meta:[
],
categories :[
14
],
标签:[
17,
18
],
},
]
有关 WP REST API 的详细信息:
我的问题是:
如何选择所有带有json的出版物?
在您的情况下 json.decode(response.body)
返回列表,而不是地图。
例如
解码JSON数组([])返回一个列表。
解码JSON对象({})返回一个Map。
因此,列表无法转换为错误所表示的Map。 / p>
要解决此问题,请这样做,
由于外部列表仅包含一个孩子,您应该这样做,
一个错误
返回新帖子。fromJson(responseJson);
正确的方式
返回新的Post.fromJson(responseJson [0]);
I would like to get all the publications of my WordPress page, and I have based using the following plugin.
This is my JSON code that is generated by the WP REST API plugin
[
{
"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
],
},
]
More information about WP REST API: https://v2.wp-api.org
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(),
);
}
}
My error with screnshoot:
My questions is: How to select all publications with json?
In your case json.decode(response.body)
returns a List, not a map.
For example
decoding JSON arrays([ ]) returns a List.
decoding JSON objects({ }) returns a Map.
Hence a List cannot be converted into a Map that's what the error says.
To resolve this do like this, Since the outer list contains only one child you should do like this,
Wrong one
return new Post.fromJson(responseJson);
Correct way
return new Post.fromJson(responseJson[0]);
这篇关于Flutter:从内容列表中获取呈现的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!