Flutter newbie - Fetching data from a JSON file with http

627

https://jsonplaceholder.typicode.com/posts, providing a list of JSON objects, so basically, we first need to parse them, follow the below example it shows how to create an object and parse it to list.

import 'dart:convert';

import 'package:http/http.dart' as http;

void main() async {
  String url =
      "https://jsonplaceholder.typicode.com/posts";
  final response = await http.get(Uri.parse(url));
  List<JsonData> list = [];
  for(var data in jsonDecode(response.body) as List) {
    list.add(JsonData.fromJson(data));
  }
  print(list[0].id); // Pass the index to get specific values from model
}

class JsonData {
  int? userId;
  int? id;
  String? title;
  String? body;

  JsonData({this.userId, this.id, this.title, this.body});

  JsonData.fromJson(Map<String, dynamic> json) {
    userId = json['userId'];
    id = json['id'];
    title = json['title'];
    body = json['body'];
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userId'] = this.userId;
    data['id'] = this.id;
    data['title'] = this.title;
    data['body'] = this.body;
    return data;
  }
}
Share:
627
boraygen
Author by

boraygen

Updated on December 30, 2022

Comments

  • boraygen
    boraygen over 1 year

    I'm new in this http and JSON world, so I may be wrong on stating or naming some terms. I'm getting a course from Udemy and I'm trying to get a specific object or that object's property. But whatever I do, I'm still fetching all the data, instead of an index.

    Here's my code:

    import 'dart:convert';
    import 'package:flutter/material.dart';
    import 'package:http/http.dart';
    
    class JsonParsingSimple extends StatefulWidget {
    
      @override
      _JsonParsingSimpleState createState() => _JsonParsingSimpleState();
    }
    
    class _JsonParsingSimpleState extends State<JsonParsingSimple> {
      Future data;
    
      @override
      void initState() {
        // TODO: implement initState
        super.initState();
        data = getData();
      }
    

    {...}

      Future getData() async
      {
        Future data;
        String url = "https://jsonplaceholder.typicode.com/posts";
        Network network = Network(url);
    
        data = network.fetchData();
        data.then((value)
        {
          print(value[0]['title']); //this line still brings me every data like i'm writing *(value)*
        });
        
        return data; 
      }
    }
    
    class Network
    {
      final String url;
      Network(this.url);
    
      Future fetchData() async
      {
        print("$url");
        Response response = await get(Uri.parse(url));
    
        if(response.statusCode == 200)
        {
          print(response.body);
          return json.decode(response.body); 
        }
        else
        {
          print(response.statusCode);
        }
      }
      
    }
    

    print(value) or print(value[0]) or print(value[0]['id']). They're all giving the same. Fetching everything.