0

我正在使用以下代码来验证库存数据:

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

输出如下所示:

I/flutter (9290): {symbol: XOM, companyName: Exxon Mobil Corp., primaryExchange: NEW YORK STOCK EXCHANGE, INC., calculatePrice: tops, open: null, openTime: null, openSource: official, close: null, closeTime : null, closeSource: 官方, high: null, highTime: 1608044090030, highSource: 15分钟延迟价格, low: null, lowTime: 1608044281245, lowSource: IEX实时价格, latestPrice: 42.52, latestSource: IEX实时价格, 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, perRatio: 54.63, week52High: 65.66, week52Low: 29.54, ytdChange: -0.3405948289133432, lastTradeTime: 1608052428625, isUSMarketOpen: true}isUSMarketOpen: true}isUSMarketOpen: true}
E/flutter(9290):[错误:flutter/lib/ui/ui_dart_state.cc(177)]未处理的异常:NoSuchMethodError:方法'[]'在null上调用。
E/flutter(9290):接收者:null
E/flutter(9290):尝试调用:

我的股票类是这样的:

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

我该如何解决这个问题?

4

1 回答 1

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 行

于 2020-12-15T17:48:13.410 回答