web-dev-qa-db-fra.com

Flutter: Comment créer un tableau avec les données JSON

Je voudrais faire un tableau avec JSON, mais je duplique le même message . J'utilise le plugin JSON REST de l'API pour WordPress

Plus d'informations sur WP REST API: https://v2.wp-api.org

Ceci est mon code 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
     ],
  },
]

Mon 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: - Le dernier généré est sélectionné, je souhaite recevoir tout le message.
J'espère que quelqu'un pourra m'aider, merci
 enter image description here

3
DomingoMG

Vous pouvez changer fetchPost pour renvoyer une liste d'articles, par exemple:

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

et vous pourriez alors utiliser le Future<List<Post>> comme ceci:

@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 prend une liste d'enfants, exactement comme Column etc. Vous pouvez donc utiliser n'importe lequel des widgets contenant une liste d'enfants.

11
Richard Heap
{
  "key": "message",
  "texts": [
    {
      "lan": "en",
      "text": "You have pushed the button this many times"
    },
    {
      "lan": "tr",
      "text": "Bu butona bir çok kez bastınız"
    },
    {
      "lan": "ru",
      "text": "Вы много раз нажимали кнопку"
    }
  ]
}

class Lang {
  final String key;
  final List<dynamic> texts;

  Lang(this.key, this.texts);

  Lang.fromJson(Map<String, dynamic> json)
      : key = json['key'],
        texts = json['texts'];

  Map<String, dynamic> toJson() => {
        'key': key,
        'texts': texts,
      };
}

class Texts {
  final String lan;
  final String text;

  Texts(this.lan, this.text);

  Texts.fromJson(Map<String, dynamic> json)
      : lan = json['lan'],
        text = json['text'];

  Map<String, dynamic> toJson() => {
        'lan': lan,
        'text': text,
      };
}


  Map<String, dynamic> languages = json.decode(jsonCrossword);
  var user = new Lang.fromJson(languages);
1
Samet öztoprak