[英]Get data from JSON in flutter
我正在使用以下代碼來驗證庫存數據:
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;
}
}
output 看起來像這樣:
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: Z37A6259CC0C1DAE29 9A7866489DFF0BDZ, 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):[錯誤:flutter/lib/ui/ui_dart_state.cc(177)]未處理的異常:NoSuchMethodError:在null上調用了方法'[]'。
E/顫振(9290):接收器:null
E/顫振(9290):嘗試調用:
我的股票 class 看起來像這樣:
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);
}
我該如何解決這個問題?
final data = json.decode(response.body) as Map;
print(data); // here the data (output below) is printed!
final quoteData = data['quote'] as Map;
鑄造是小鬼,試試這個,也讓我知道..只替換上面的 3 行
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.