【问题标题】:Get data from JSON in flutter在颤振中从 JSON 中获取数据
【发布时间】:2021-03-26 06:13:26
【问题描述】:

我正在使用以下代码来验证股票数据:

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, extendedP 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:306838479,iexBidPriceS: :100,iexAskPrice:42.51,iexAskSize:400,iexOpen:42.475,iexOpenTime:1608052428625,iexClose:42.475,iexCloseTime:1608052428625,marketCap:179594243992,操作: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):接收器:空
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);
}

我该如何解决这个问题?

【问题讨论】:

  • 请分享您的整个 JSON,您提供的输出已被剪切,因为打印方法有限制。通过使用调试模式并在分配变量后立即设置断点,您可以轻松获取整个变量内容。
  • 我编辑了输出!
  • data['quote'] 不是一个东西,因此,它是null

标签: json flutter dart fromjson


【解决方案1】:
final data = json.decode(response.body) as Map;

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

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

铸造是小鬼,试试这个,也让我知道.. 只替换上面的 3 行

【讨论】:

    猜你喜欢
    • 2019-04-20
    • 2020-11-17
    • 2021-03-21
    • 2021-08-11
    • 2021-01-26
    • 2020-03-19
    • 2019-11-21
    • 2021-03-17
    • 2022-10-06
    相关资源
    最近更新 更多