Flutter: How to Make an array with the JSON data

You could change fetchPost to return a List of Posts, like:

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

and you could then utilize the Future<List<Post>> like this:

@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 takes a List of children, just like Column etc. So you could use any of the Widgets that enclose a list of children


{
  "key": "message",
  "texts": [
    {
      "lan": "en",
      "text": "You have pushed this button 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);

Tags:

Dart

Flutter