Get data from JSON in flutter

106 views Asked by At

I am using the following code to verify stock data:

Future<Stock> verifyIfStockSymbolIsValid(String symbol) async {
  final response =
  await http.get('https://cloud.iexapis.com/stable/stock/$symbol/quote?token=my-token');
  if (response.statusCode == 200) {
    // Get Map from JSON.
    Map data = json.decode(response.body);
    print(data); // here the data (output below) is printed!

    Map quoteData = data['quote'];
    Stock stockQuote = Stock.fromJson(quoteData); // here should be the problem
 
    return stockQuote;
  } else {
    return null;
  }
}

The output looks like this:

I/flutter ( 9290): {symbol: XOM, companyName: Exxon Mobil Corp., primaryExchange: NEW YORK STOCK EXCHANGE, INC., calculationPrice: tops, open: null, openTime: null, openSource: official, close: null, closeTime: null, closeSource: official, high: null, highTime: 1608044090030, highSource: 15 minute delayed price, low: null, lowTime: 1608044281245, lowSource: IEX real time price, latestPrice: 42.52, latestSource: IEX real time price, latestTime: 10:09:34 AM, latestUpdate: 1608044974460, latestVolume: null, iexRealtimePrice: 42.52, iexRealtimeSize: 100, iexLastUpdated: 1608044974460, delayedPrice: null, delayedPriceTime: null, oddLotDelayedPrice: null, oddLotDelayedPriceTime: null, extendedPrice: null, extendedChange: null, extendedChangePercent: null, extendedPriceTime: null, previousClose: 42.22, previousVolume: 30595042, change: 0.3, changePercent: 0.00711, volume: null, iexMarketPercent: 0.01788127392568208, iexVolume: 65063, avgTotalVolume: 30683847, iexBidPrice: 41.99, iexBidSize: 100, iexAskPrice: 42.51, iexAskSize: 400, iexOpen: 42.475, iexOpenTime: 1608052428625, iexClose: 42.475, iexCloseTime: 1608052428625, marketCap: 179594243992, peRatio: 54.63, week52High: 65.66, week52Low: 29.54, ytdChange: -0.3405948289133432, lastTradeTime: 1608052428625, isUSMarketOpen: true}
E/flutter ( 9290): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter ( 9290): Receiver: null
E/flutter ( 9290): Tried calling:

And my stock class is looking like that:

class Stock {

  final String companyName;
  final String symbol;

  // The following are dynamic as using double generates an error
  // when the API returns values with no decimal (e.g. 12 vs 12.0).
  final dynamic latestPrice;
  final dynamic low;
  final dynamic high;
  final dynamic week52High;
  final dynamic change;
  final dynamic changePercent;
  final dynamic peRatio;
  final dynamic previousClose;

  // Default constructor.
  Stock(this.companyName, this.symbol, this.latestPrice,
  this.low, this.high, this.week52High, this.change,
  this.changePercent, this.peRatio, this.previousClose);

  // Named constructor, create object from JSON.
  Stock.fromJson(Map<String, dynamic> json)
  : companyName = (json['companyName'] != null ? json['companyName'] : ""),
    symbol = (json['symbol'] != null ? json['symbol'] : ""),
    latestPrice = (json['latestPrice'] != null ? json['latestPrice'] : 0.0),
    low = (json['low'] != null ? json['low'] : 0.0),
    high = (json['high'] != null ? json['high'] : 0.0),
    week52High = (json['week52High'] != null ? json['week52High'] : 0.0),
    change = (json['change'] != null ? json['change'] : 0.0),
    changePercent = (json['changePercent'] != null ? json['changePercent'] : 0.0),
    peRatio = (json['peRatio'] != null ? json['peRatio'] : 0.0),
    previousClose = (json['previousClose'] != null ? json['previousClose'] : 0.0);
}

How can I solve this?

1

There are 1 answers

0
Milind Gour On
final data = json.decode(response.body) as Map;

print(data); // here the data (output below) is printed!

final  quoteData = data['quote'] as Map;

Casting is imp , try this and let me know too.. replace only 3 lines above