颤动:从内容中获取渲染文本

时间:2018-06-04 12:07:51

标签: dart flutter

这是带有WP(JSON)API插件的帖子(wordpress)的内容:

"content": {
   "rendered": "<p>Content post.<\/p>\n", <---- I want retrieve this
   "protected": false
}

我该怎么办? (与您的示例一起使用 - &gt; https://flutter.io/cookbook/networking/fetch-data/

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('http://***********:88/WordPress/wp-json/wp/v2/posts/65');
  final responseJson = json.decode(response.body);

  return new Post.fromJson(responseJson);
}

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

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


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

我的应用中的问题: enter image description here

1 个答案:

答案 0 :(得分:1)

你这样做:

body: json['content'].toString(),

但是如果你只想要rendered属性,那么应该是这样的:

body: json['content']['rendered'].toString(),

但是,由于它有HTML,您可能还需要flutter_html_view之类的内容来呈现HTML。