Search code examples
jsonflutter

Flutter: JSON response incomplete


I'm trying to get weekly stock data from an API in Flutter. However, the response is cut off quite early. What am I missing?

static Future<List<StockWeek>> fetchAll() async {
    // creates: https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY&symbol=MSFT&apikey=demo
    var uri = Endpoint.uri('', queryParameters: { "function" : "TIME_SERIES_WEEKLY", "symbol" : "MSFT", "apikey" : "demo" });

    final resp = await http.get(uri.toString());

    if (resp.statusCode != 200) {
      throw (resp.body);
    }

    print(resp.body.toString());

    List<StockWeek> list = new List<StockWeek>();

    // parse JSON

    return list;
  }

What I'm getting at the time of the print statement is this:

I/flutter (19224): {
I/flutter (19224):     "Meta Data": {
I/flutter (19224):         "1. Information": "Weekly Prices (open, high, low, close) and Volumes",
I/flutter (19224):         "2. Symbol": "MSFT",
I/flutter (19224):         "3. Last Refreshed": "2018-12-03",
I/flutter (19224):         "4. Time Zone": "US/Eastern"
I/flutter (19224):     },
I/flutter (19224):     "Weekly Time Series": {
I/flutter (19224):         "2018-12-03": {
I/flutter (19224):             "1. open": "113.0000",
I/flutter (19224):             "2. high": "113.4200",
I/flutter (19224):             "3. low": "110.7300",
I/flutter (19224):             "4. close": "112.0900",
I/flutter (19224):             "5. volume": "34275048"
I/flutter (19224):         },
I/flutter (19224):         "2018-11-30": {
I/flutter (19224):             "1. open": "104.7900",
I/flutter (19224):             "2. high": "111.3300",
I/flutter (19224):             "3. low": "104.5800",
I/flutter (19224):             "4. close": "110.8900",
I/flutter (19224):             "5. volume": "170037931"
I/flutter (19224):         },
I/flutter (19224):         "2018-11-23": {
I/flutter (19224):             "1. open": "108.2700",
I/flutter (19224):             "2. high": "108.5600",
I/flutter (19224):             "3. low": "99.3528",
I/flutter (19224):             "4. close": "103.0700",
I/flutter (19224):             "5. volume": "150780076"
I/flutter (19224):         },
I/flutter (19224):         "2018-11-16": {
I/flutter (19224):             "1. open": "109.4200",
I/flutter (19224):             "2. high": "109.9600",
I/flutter (19224):             "3. low": "103.9100",
I/flutter (19224):             "4. 

It consistently stops at this point. As opposed to what the reply should look like:

https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY&symbol=MSFT&apikey=demo


Solution

  • This is just a logging issue when using print() with a lot of lines. Use debugPrint() from Flutter’s foundation library instead as recommended here.

    import 'package:flutter/foundation.dart'; 
    
    static Future<List<StockWeek>> fetchAll() async {
        // creates: https://www.alphavantage.co/query?function=TIME_SERIES_WEEKLY&symbol=MSFT&apikey=demo
        var uri = Endpoint.uri('', queryParameters: { "function" : "TIME_SERIES_WEEKLY", "symbol" : "MSFT", "apikey" : "demo" });
    
        final resp = await http.get(uri.toString());
    
        if (resp.statusCode != 200) {
          throw (resp.body);
        }
    
        debugPrint(resp.body.toString());
    
        List<StockWeek> list = new List<StockWeek>();
    
        // parse JSON
    
        return list;   
    }