简体   繁体   English

Dart 解析嵌套地图 json

[英]Dart parse nested map json

I would like to map each of the attributes eg cashAndShortTermInvestments, accountsReceivable from below site in a Model class when parsing the json.我想在解析 json 时映射模型类中的每个属性,例如 cashAndShortTermInvestments、accountReceivable 从下面的站点。 Below are my codes and it doesn't seem to work.下面是我的代码,它似乎不起作用。 Appreciate if someone could help point where my codes are wrong and how it should be corrected.感谢有人可以帮助指出我的代码错误的地方以及应该如何更正。

Site: https://dev.last10k.com/docs/services/53c7e3aafd2dff034f040002/operations/5e2a9d30c8fa8071c307f0e2 ?网站: https : //dev.last10k.com/docs/services/53c7e3aafd2dff034f040002/operations/5e2a9d30c8fa8071c307f0e2 ?

Thank you very much in advance!非常感谢您提前!

class Financial {
  String name;
  Map<String, double> historical;
  Map<String, double>? recent;
  Map<String, String> category;

  Financial({
    required this.name,
    required this.historical,
    this.recent,
    required this.category,
  });

  factory Financial.fromJson(Map<String, dynamic> json) {
    return Financial(
      name: json['Name'],
      historical: Map.from(json['Historical']).map((k, v) =>
          MapEntry<String, double>(k, v.fromJson(v))),
      recent: json["Recent"] != null ? Map.from(json['Recent']).map((k, v) =>
          MapEntry<String, double>(k, v.fromJson(v))) : <String, double>{},
      category: Map.from(json['Category']).map((k, v) =>
          MapEntry<String, String>(k, v.fromJson(v))),
    );
  }

  @override
  String toString() {
    return 'Financial: {name = $name, '
        'historical: $historical, '
        'recent: $recent, '
        'category: $category }';
  }
} // class Financials

class RatiosModel {
  final Financial cashAndShortTermInvestments;
  final Financial accountsReceivable;
  final Financial inventory;
  final Financial otherCurrentAssets;
  final Financial totalCurrentAssets;
  final Financial netPPE;
  final Financial intangibles;
  final Financial otherLongTermAssets;
  final Financial totalAssets;
  final Financial accountsPayable;
  final Financial shortTermDebt;
  final Financial taxesPayable;
  final Financial accruedLiabilities;
  final Financial otherShortTermLiabilities;
  final Financial totalCurrentLiabilities;
  final Financial longTermDebt;
  final Financial otherLongTermLiabilities;
  final Financial totalLiabilities;
  final Financial totalStockholdersEquity;
  final Financial totalLiabilitiesEquity; // data probably no use
  final Financial currentRatio;
  final Financial quickRatio;
  final Financial financialLeverage;
  final Financial debtEquity;
  final Financial daysSalesOutstanding;
  final Financial daysInventory;
  final Financial payablesPeriod;
  final Financial cashConversionCycle;
  final Financial receivablesTurnover;
  final Financial inventoryTurnover;
  final Financial fixedAssetsTurnover;
  final Financial assetTurnover;
  final Financial bookValuePerShare;
  final Financial capSpending;
  final Financial dividends;
  final Financial earningsPerShare;
  final Financial freeCashFlowPerShare;
  final Financial freeCashFlow;
  final Financial grossMargin;
  final Financial netIncome;
  final Financial operatingCashFlow;
  final Financial operatingIncome;
  final Financial operatingMargin;
  final Financial payoutRatio;
  final Financial revenue;
  final Financial shares;
  final Financial workingCapital;
  final Financial cogs;
  final Financial grossSalesMargin;
  final Financial ebtMargin;
  final Financial netIntIncOther;
  final Financial otherSalesMargins;
  final Financial operationSalesMargin;
  final Financial researchAndDevelopment;
  final Financial sga;
  final Financial salesRevenue;
  final Financial assetTurnoverAverage;
  final Financial financialLeverageAverage;
  final Financial interestCoverage;
  final Financial netMargin;
  final Financial returnOnAssets;
  final Financial returnOnInvestedCapital;
  final Financial taxRate;
  final Financial revenueFiveYearAverage;
  final Financial revenueThreeYearAverage;
  final Financial revenueTenYearAverage;
  final Financial revenueYearOverYear;
  final Financial operatingIncomeFiveYearAverage;
  final Financial operatingIncomeTenYearAverage;
  final Financial operatingIncomeThreeYearAverage;
  final Financial operatingIncomeYearOverYear;
  final Financial netIncomeFiveYearAverage;
  final Financial netIncomeTenYearAverage;
  final Financial netIncomeThreeYearAverage;
  final Financial netIncomeYearOverYear;
  final Financial epsFiveYearAverage;
  final Financial epsTenYearAverage;
  final Financial epsThreeYearAverage;
  final Financial epsYearOverYear;
  final Financial operatingCashFlowYearOverYear;
  final Financial freeCashFlowYearOverYear;
  final Financial capExSales;
  final Financial freeCashFlowSales;
  final Financial freeCashFlowNetIncome;

  RatiosModel ({
    required this.cashAndShortTermInvestments,
    required this.accountsReceivable,
    required this.inventory,
    required this.otherCurrentAssets,
    required this.totalCurrentAssets,
    required this.netPPE,
    required this.intangibles,
    required this.otherLongTermAssets,
    required this.totalAssets,
    required this.accountsPayable,
    required this.shortTermDebt,
    required this.taxesPayable,
    required this.accruedLiabilities,
    required this.otherShortTermLiabilities,
    required this.totalCurrentLiabilities,
    required this.longTermDebt,
    required this.otherLongTermLiabilities,
    required this.totalLiabilities,
    required this.totalStockholdersEquity,
    required this.totalLiabilitiesEquity, // data probably no use
    required this.currentRatio,
    required this.quickRatio,
    required this.financialLeverage,
    required this.debtEquity,
    required this.daysSalesOutstanding,
    required this.daysInventory,
    required this.payablesPeriod,
    required this.cashConversionCycle,
    required this.receivablesTurnover,
    required this.inventoryTurnover,
    required this.fixedAssetsTurnover,
    required this.assetTurnover,
    required this.bookValuePerShare,
    required this.capSpending,
    required this.dividends,
    required this.earningsPerShare,
    required this.freeCashFlowPerShare,
    required this.freeCashFlow,
    required this.grossMargin,
    required this.netIncome,
    required this.operatingCashFlow,
    required this.operatingIncome,
    required this.operatingMargin,
    required this.payoutRatio,
    required this.revenue,
    required this.shares,
    required this.workingCapital,
    required this.cogs,
    required this.grossSalesMargin,
    required this.ebtMargin,
    required this.netIntIncOther,
    required this.otherSalesMargins,
    required this.operationSalesMargin,
    required this.researchAndDevelopment,
    required this.sga,
    required this.salesRevenue,
    required this.assetTurnoverAverage,
    required this.financialLeverageAverage,
    required this.interestCoverage,
    required this.netMargin,
    required this.returnOnAssets,
    required this.returnOnInvestedCapital,
    required this.taxRate,
    required this.revenueFiveYearAverage,
    required this.revenueThreeYearAverage,
    required this.revenueTenYearAverage,
    required this.revenueYearOverYear,
    required this.operatingIncomeFiveYearAverage,
    required this.operatingIncomeTenYearAverage,
    required this.operatingIncomeThreeYearAverage,
    required this.operatingIncomeYearOverYear,
    required this.netIncomeFiveYearAverage,
    required this.netIncomeTenYearAverage,
    required this.netIncomeThreeYearAverage,
    required this.netIncomeYearOverYear,
    required this.epsFiveYearAverage,
    required this.epsTenYearAverage,
    required this.epsThreeYearAverage,
    required this.epsYearOverYear,
    required this.operatingCashFlowYearOverYear,
    required this.freeCashFlowYearOverYear,
    required this.capExSales,
    required this.freeCashFlowSales,
    required this.freeCashFlowNetIncome
  });

  factory RatiosModel.fromJson(Map<String, dynamic> json) {
    return RatiosModel(
      cashAndShortTermInvestments: json["CashAndShortTermInvestments"],
      accountsReceivable: json["AccountsReceivable"],
      inventory: json["Inventory"],
      otherCurrentAssets: json["OtherCurrentAssets"],
      totalCurrentAssets: json["TotalCurrentAssets"],
      netPPE: json["netPPE"],
      intangibles: json["Intangibles"],
      otherLongTermAssets: json["OtherLongTermAssets"],
      totalAssets: json["TotalAssets"],
      accountsPayable: json["AccountsPayable"],
      shortTermDebt: json["ShortTermDebt"],
      taxesPayable: json["TaxesPayable"],
      accruedLiabilities: json["AccruedLiabilities"],
      otherShortTermLiabilities: json["OtherShortTermLiabilities"],
      totalCurrentLiabilities: json["TotalCurrentLiabilities"],
      longTermDebt: json["LongTermDebt"],
      otherLongTermLiabilities: json["OtherLongTermLiabilities"],
      totalLiabilities: json["TotalLiabilities"],
      totalStockholdersEquity: json["TotalStockholdersEquity"],
      totalLiabilitiesEquity: json["TotalLiabilitiesEquity"], // data probably no use
      currentRatio: json["CurrentRatio"],
      quickRatio: json["QuickRatio"],
      financialLeverage: json["FinancialLeverage"],
      debtEquity: json["DebtEquity"],
      daysSalesOutstanding: json["DaysSalesOutstanding"],
      daysInventory: json["DaysInventory"],
      payablesPeriod: json["PayablesPeriod"],
      cashConversionCycle: json["CashConversionCycle"],
      receivablesTurnover: json["ReceivablesTurnover"],
      inventoryTurnover: json["InventoryTurnover"],
      fixedAssetsTurnover: json["FixedAssetsTurnover"],
      assetTurnover: json["AssetTurnover"],
      bookValuePerShare: json["BookValuePerShare"],
      capSpending: json["CapSpending"],
      dividends: json["Dividends"],
      earningsPerShare: json["EarningsPerShare"],
      freeCashFlowPerShare: json["FreeCashFlowPerShare"],
      freeCashFlow: json["FreeCashFlow"],
      grossMargin: json["GrossMargin"],
      netIncome: json["NetIncome"],
      operatingCashFlow: json["OperatingCashFlow"],
      operatingIncome: json["OperatingIncome"],
      operatingMargin: json["OperatingMargin"],
      payoutRatio: json["PayoutRatio"],
      revenue: json["Revenue"],
      shares: json["Shares"],
      workingCapital: json["WorkingCapital"],
      cogs: json["COGS"],
      grossSalesMargin: json["GrossSalesMargin"],
      ebtMargin: json["EBTMargin"],
      netIntIncOther: json["NetIntIncOther"],
      otherSalesMargins: json["OtherSalesMargins"],
      operationSalesMargin: json["OperationSalesMargin"],
      researchAndDevelopment: json["ResearchAndDevelopment"],
      sga: json["SGA"],
      salesRevenue: json["SalesRevenue"],
      assetTurnoverAverage: json["AssetTurnoverAverage"],
      financialLeverageAverage: json["FinancialLeverageAverage"],
      interestCoverage: json["InterestCoverage"],
      netMargin: json["NetMargin"],
      returnOnAssets: json["ReturnOnAssets"],
      returnOnInvestedCapital: json["ReturnOnInvestedCapital"],
      taxRate: json["TaxRate"],
      revenueFiveYearAverage: json["RevenueFiveYearAverage"],
      revenueThreeYearAverage: json["RevenueThreeYearAverage"],
      revenueTenYearAverage: json["RevenueTenYearAverage"],
      revenueYearOverYear: json["RevenueYearOverYear"],
      operatingIncomeFiveYearAverage: json["OperatingIncomeFiveYearAverage"],
      operatingIncomeTenYearAverage: json["OperatingIncomeTenYearAverage"],
      operatingIncomeThreeYearAverage: json["OperatingIncomeThreeYearAverage"],
      operatingIncomeYearOverYear: json["OperatingIncomeYearOverYear"],
      netIncomeFiveYearAverage: json["NetIncomeFiveYearAverage"],
      netIncomeTenYearAverage: json["NetIncomeTenYearAverage"],
      netIncomeThreeYearAverage: json["NetIncomeThreeYearAverage"],
      netIncomeYearOverYear: json["NetIncomeYearOverYear"],
      epsFiveYearAverage: json["EPSFiveYearAverage"],
      epsTenYearAverage: json["EPSTenYearAverage"],
      epsThreeYearAverage: json["EPSThreeYearAverage"],
      epsYearOverYear: json["EPSYearOverYear"],
      operatingCashFlowYearOverYear: json["OperatingCashFlowYearOverYear"],
      freeCashFlowYearOverYear: json["FreeCashFlowYearOverYear"],
      capExSales: json["CapExSales"],
      freeCashFlowSales: json["FreeCashFlowSales"],
      freeCashFlowNetIncome: json["FreeCashFlowNetIncome"],
    );
  } // factory TimeSeriesMetaData.fromJson

  @override
  String toString() {
    return 'RatiosModel: {cashAndShortTermInvestments = $cashAndShortTermInvestments, '
        'accountsReceivable = $accountsReceivable, inventory = $inventory, '
        'otherCurrentAssets = $otherCurrentAssets, totalCurrentAssets = $totalCurrentAssets, '
        'netPPE= $netPPE, intangibles = $intangibles, otherLongTermAssets = $otherLongTermAssets, '
        'totalAssets = $totalAssets, accountsPayable = $accountsPayable, '
        'shortTermDebt = $shortTermDebt, taxesPayable = $taxesPayable, '
        'accruedLiabilities = $accruedLiabilities, otherShortTermLiabilities = $otherShortTermLiabilities, '
        'totalCurrentLiabilities = $totalCurrentLiabilities, longTermDebt = $longTermDebt, '
        'otherLongTermLiabilities = $otherLongTermLiabilities, totalLiabilities = $totalLiabilities, '
        'totalStockholdersEquity = $totalStockholdersEquity, totalLiabilitiesEquity = $totalLiabilitiesEquity, '
        'currentRatio = $currentRatio, quickRatio = $quickRatio, '
        'financialLeverage = $financialLeverage, debtEquity = $debtEquity, '
        'daysSalesOutstanding = $daysSalesOutstanding, daysInventory = $daysInventory, '
        'payablesPeriod = $payablesPeriod, cashConversionCycle = $cashConversionCycle, '
        'receivablesTurnover = $receivablesTurnover, inventoryTurnover = $inventoryTurnover, '
        'fixedAssetsTurnover = $fixedAssetsTurnover, assetTurnover = $assetTurnover, '
        'bookValuePerShare = $bookValuePerShare, capSpending = $capSpending, dividends = $dividends, '
        'earningsPerShare = $earningsPerShare, freeCashFlowPerShare = $freeCashFlowPerShare, '
        'freeCashFlow = $freeCashFlow, grossMargin = $grossMargin, netIncome = $netIncome, '
        'operatingIncome = $operatingIncome, operatingMargin = $operatingMargin, payoutRatio = $payoutRatio, '
        'payoutRatio = $payoutRatio, revenue = $revenue, shares = $shares, '
        'workingCapital = $workingCapital, cogs = $cogs, grossSalesMargin = $grossSalesMargin, '
        'ebtMargin = $ebtMargin, netIntIncOther = $netIntIncOther, otherSalesMargins = $otherSalesMargins, '
        'operationSalesMargin = $operationSalesMargin, researchAndDevelopment = $researchAndDevelopment, '
        'sga = $sga, salesRevenue = $salesRevenue, assetTurnoverAverage = $assetTurnoverAverage, '
        'financialLeverageAverage = $financialLeverageAverage, interestCoverage = $interestCoverage, '
        'netMargin = $netMargin, returnOnAssets = $returnOnAssets, '
        'returnOnInvestedCapital = $returnOnInvestedCapital, taxRate = $taxRate, '
        'revenueFiveYearAverage = $revenueFiveYearAverage, revenueThreeYearAverage = $revenueThreeYearAverage, '
        'revenueTenYearAverage = $revenueTenYearAverage, revenueYearOverYear = $revenueYearOverYear, '
        'operatingIncomeFiveYearAverage = $operatingIncomeFiveYearAverage, '
        'operatingIncomeTenYearAverage = $operatingIncomeTenYearAverage, '
        'operatingIncomeThreeYearAverage = $operatingIncomeThreeYearAverage, '
        'operatingIncomeYearOverYear = $operatingIncomeYearOverYear, '
        'netIncomeFiveYearAverage = $netIncomeFiveYearAverage, netIncomeTenYearAverage = $netIncomeTenYearAverage, '
        'netIncomeThreeYearAverage = $netIncomeThreeYearAverage, netIncomeYearOverYear = $netIncomeYearOverYear, '
        'epsFiveYearAverage = $epsFiveYearAverage, epsTenYearAverage = $epsTenYearAverage, '
        'epsThreeYearAverage = $epsThreeYearAverage, epsYearOverYear = $epsYearOverYear, '
        'operatingCashFlowYearOverYear = $operatingCashFlowYearOverYear, '
        'freeCashFlowYearOverYear = $freeCashFlowYearOverYear, capExSales = $capExSales, '
        'freeCashFlowSales = $freeCashFlowSales, freeCashFlowNetIncome }';
  } // toString
} // class RatiosModel


import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:flutter/services.dart' show rootBundle;

void main() async { 
  runApp(MyApp());
} // main

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Global Fleas',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.indigo,
        brightness: Brightness.dark,
      ),
      home: Test(),
    );
  } // Widget build(BuildContext context)
}
 
class Test extends StatefulWidget {
  Test({Key? key/*, this.title*/}) : super(key: key);
  //final String title;
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  late Future <RatiosModel> _ratios;

  void initState() {
    super.initState();
    _ratios = _getRatios();
    //_getRatios();
  } // void initState()

  Future <RatiosModel> _getRatios() async {
    String response = await rootBundle.loadString('assets/last10k_ratios.json');
    dynamic jsonObject = json.decode(response);
    final convertedJsonObject = jsonObject.cast<dynamic, dynamic>();
    print('convertedJsonObject: ${convertedJsonObject["data"]["attributes"]["result"]}');
    RatiosModel ratios =
        convertedJsonObject["data"]["attributes"]["result"].map<RatiosModel>((json) =>
            RatiosModel.fromJson(json));
    print('ratios: $ratios');
    return ratios;
  } // Future <RatiosModel> _getRatios() async

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: FutureBuilder( // Day Performance - Dow Jones
          future: _ratios,
          builder: (BuildContext context, AsyncSnapshot<RatiosModel> snapshot,)
          {
            if (snapshot.connectionState == ConnectionState.done) {
              if (snapshot.hasError) {
                return const Text('Error');
              } else if (snapshot.hasData) {
                return Text('test');//_getBodyWidget(snapshot.data!);
              } else {
                return const Text('No data');
              }
            } else {
              return CircularProgressIndicator();//Text('State: ${snapshot.connectionState}');
            }
          }),
    );
  } // Widget build(BuildContext context)

}

i found the issue.. i should have done below for each attribute in factory RatiosModel constructor我发现了这个问题.. 我应该为工厂 RatiosModel 构造函数中的每个属性做以下操作

cashAndShortTermInvestments: Financial.fromJson(json["CashAndShortTermInvestments"]),

also, i have changed the parameter of the factory constructor to (dynamic json)另外,我已将工厂构造函数的参数更改为(动态 json)

i also corrected below in factory Financial.fromJson(dynamic json)我还在下面的工厂 Financial.fromJson(dynamic json) 中更正了

historical: Map.from(json['Historical']).map((k, v) =>
          MapEntry<String, double>(k, v)),

category: Map.from(json['Category']).map((k, v) =>
          MapEntry<String, String>(k, v)),


class Financial {
  String name;
  Map<String, double> historical;
  Map<String, double>? recent;
  Map<String, String> category;

  Financial({
    required this.name,
    required this.historical,
    this.recent,
    required this.category,
  });

  factory Financial.fromJson(dynamic json) {
    return Financial(
      name: json['Name'],
      historical: Map.from(json['Historical']).map((k, v) =>
          MapEntry<String, double>(k, v)),
      recent: json["Recent"] != null ? Map.from(json['Recent']).map((k, v) =>
          MapEntry<String, double>(k, v)) : <String, double>{},
      category: Map.from(json['Category']).map((k, v) =>
          MapEntry<String, String>(k, v)),
    );
  }

  @override
  String toString() {
    return 'Financial: {name = $name, '
        'historical: $historical, '
        'recent: $recent, '
        'category: $category }';
  }
} // class Financials

class RatiosModel {
  final Financial cashAndShortTermInvestments;
  final Financial accountsReceivable;
  final Financial inventory;
  final Financial otherCurrentAssets;
  final Financial totalCurrentAssets;
  final Financial netPPE;
  final Financial intangibles;
  final Financial otherLongTermAssets;
  final Financial totalAssets;
  final Financial accountsPayable;
  final Financial shortTermDebt;
  final Financial taxesPayable;
  final Financial accruedLiabilities;
  final Financial otherShortTermLiabilities;
  final Financial totalCurrentLiabilities;
  final Financial longTermDebt;
  final Financial otherLongTermLiabilities;
  final Financial totalLiabilities;
  final Financial totalStockholdersEquity;
  final Financial totalLiabilitiesEquity; // data probably no use
  final Financial currentRatio;
  final Financial quickRatio;
  final Financial financialLeverage;
  final Financial debtEquity;
  final Financial daysSalesOutstanding;
  final Financial daysInventory;
  final Financial payablesPeriod;
  final Financial cashConversionCycle;
  final Financial receivablesTurnover;
  final Financial inventoryTurnover;
  final Financial fixedAssetsTurnover;
  final Financial assetTurnover;
  final Financial bookValuePerShare;
  final Financial capSpending;
  final Financial dividends;
  final Financial earningsPerShare;
  final Financial freeCashFlowPerShare;
  final Financial freeCashFlow;
  final Financial grossMargin;
  final Financial netIncome;
  final Financial operatingCashFlow;
  final Financial operatingIncome;
  final Financial operatingMargin;
  final Financial payoutRatio;
  final Financial revenue;
  final Financial shares;
  final Financial workingCapital;
  final Financial cogs;
  final Financial grossSalesMargin;
  final Financial ebtMargin;
  final Financial netIntIncOther;
  final Financial otherSalesMargins;
  final Financial operationSalesMargin;
  final Financial researchAndDevelopment;
  final Financial sga;
  final Financial salesRevenue;
  final Financial assetTurnoverAverage;
  final Financial financialLeverageAverage;
  final Financial interestCoverage;
  final Financial netMargin;
  final Financial returnOnAssets;
  final Financial returnOnInvestedCapital;
  final Financial taxRate;
  final Financial revenueFiveYearAverage;
  final Financial revenueThreeYearAverage;
  final Financial revenueTenYearAverage;
  final Financial revenueYearOverYear;
  final Financial operatingIncomeFiveYearAverage;
  final Financial operatingIncomeTenYearAverage;
  final Financial operatingIncomeThreeYearAverage;
  final Financial operatingIncomeYearOverYear;
  final Financial netIncomeFiveYearAverage;
  final Financial netIncomeTenYearAverage;
  final Financial netIncomeThreeYearAverage;
  final Financial netIncomeYearOverYear;
  final Financial epsFiveYearAverage;
  final Financial epsTenYearAverage;
  final Financial epsThreeYearAverage;
  final Financial epsYearOverYear;
  final Financial operatingCashFlowYearOverYear;
  final Financial freeCashFlowYearOverYear;
  final Financial capExSales;
  final Financial freeCashFlowSales;
  final Financial freeCashFlowNetIncome;

  RatiosModel ({
    required this.cashAndShortTermInvestments,
    required this.accountsReceivable,
    required this.inventory,
    required this.otherCurrentAssets,
    required this.totalCurrentAssets,
    required this.netPPE,
    required this.intangibles,
    required this.otherLongTermAssets,
    required this.totalAssets,
    required this.accountsPayable,
    required this.shortTermDebt,
    required this.taxesPayable,
    required this.accruedLiabilities,
    required this.otherShortTermLiabilities,
    required this.totalCurrentLiabilities,
    required this.longTermDebt,
    required this.otherLongTermLiabilities,
    required this.totalLiabilities,
    required this.totalStockholdersEquity,
    required this.totalLiabilitiesEquity, // data probably no use
    required this.currentRatio,
    required this.quickRatio,
    required this.financialLeverage,
    required this.debtEquity,
    required this.daysSalesOutstanding,
    required this.daysInventory,
    required this.payablesPeriod,
    required this.cashConversionCycle,
    required this.receivablesTurnover,
    required this.inventoryTurnover,
    required this.fixedAssetsTurnover,
    required this.assetTurnover,
    required this.bookValuePerShare,
    required this.capSpending,
    required this.dividends,
    required this.earningsPerShare,
    required this.freeCashFlowPerShare,
    required this.freeCashFlow,
    required this.grossMargin,
    required this.netIncome,
    required this.operatingCashFlow,
    required this.operatingIncome,
    required this.operatingMargin,
    required this.payoutRatio,
    required this.revenue,
    required this.shares,
    required this.workingCapital,
    required this.cogs,
    required this.grossSalesMargin,
    required this.ebtMargin,
    required this.netIntIncOther,
    required this.otherSalesMargins,
    required this.operationSalesMargin,
    required this.researchAndDevelopment,
    required this.sga,
    required this.salesRevenue,
    required this.assetTurnoverAverage,
    required this.financialLeverageAverage,
    required this.interestCoverage,
    required this.netMargin,
    required this.returnOnAssets,
    required this.returnOnInvestedCapital,
    required this.taxRate,
    required this.revenueFiveYearAverage,
    required this.revenueThreeYearAverage,
    required this.revenueTenYearAverage,
    required this.revenueYearOverYear,
    required this.operatingIncomeFiveYearAverage,
    required this.operatingIncomeTenYearAverage,
    required this.operatingIncomeThreeYearAverage,
    required this.operatingIncomeYearOverYear,
    required this.netIncomeFiveYearAverage,
    required this.netIncomeTenYearAverage,
    required this.netIncomeThreeYearAverage,
    required this.netIncomeYearOverYear,
    required this.epsFiveYearAverage,
    required this.epsTenYearAverage,
    required this.epsThreeYearAverage,
    required this.epsYearOverYear,
    required this.operatingCashFlowYearOverYear,
    required this.freeCashFlowYearOverYear,
    required this.capExSales,
    required this.freeCashFlowSales,
    required this.freeCashFlowNetIncome
  });

  factory RatiosModel.fromJson(dynamic json) {
    Financial test = Financial.fromJson(json["CashAndShortTermInvestments"]);
    print('test: $test');
    return RatiosModel(
      cashAndShortTermInvestments: Financial.fromJson(json["CashAndShortTermInvestments"]),
      accountsReceivable: Financial.fromJson(json["AccountsReceivable"]),
      inventory: Financial.fromJson(json["Inventory"]),
      otherCurrentAssets: Financial.fromJson(json["OtherCurrentAssets"]),
      totalCurrentAssets: Financial.fromJson(json["TotalCurrentAssets"]),
      netPPE: Financial.fromJson(json["NetPPE"]),
      intangibles: Financial.fromJson(json["Intangibles"]),
      otherLongTermAssets: Financial.fromJson(json["OtherLongTermAssets"]),
      totalAssets: Financial.fromJson(json["TotalAssets"]),
      accountsPayable: Financial.fromJson(json["AccountsPayable"]),
      shortTermDebt: Financial.fromJson(json["ShortTermDebt"]),
      taxesPayable: Financial.fromJson(json["TaxesPayable"]),
      accruedLiabilities: Financial.fromJson(json["AccruedLiabilities"]),
      otherShortTermLiabilities: Financial.fromJson(json["OtherShortTermLiabilities"]),
      totalCurrentLiabilities: Financial.fromJson(json["TotalCurrentLiabilities"]),
      longTermDebt: Financial.fromJson(json["LongTermDebt"]),
      otherLongTermLiabilities: Financial.fromJson(json["OtherLongTermLiabilities"]),
      totalLiabilities: Financial.fromJson(json["TotalLiabilities"]),
      totalStockholdersEquity: Financial.fromJson(json["TotalStockholdersEquity"]),
      totalLiabilitiesEquity: Financial.fromJson(json["TotalLiabilitiesEquity"]), // data probably no use
      currentRatio: Financial.fromJson(json["CurrentRatio"]),
      quickRatio: Financial.fromJson(json["QuickRatio"]),
      financialLeverage: Financial.fromJson(json["FinancialLeverage"]),
      debtEquity: Financial.fromJson(json["DebtEquity"]),
      daysSalesOutstanding: Financial.fromJson(json["DaysSalesOutstanding"]),
      daysInventory: Financial.fromJson(json["DaysInventory"]),
      payablesPeriod: Financial.fromJson(json["PayablesPeriod"]),
      cashConversionCycle: Financial.fromJson(json["CashConversionCycle"]),
      receivablesTurnover: Financial.fromJson(json["ReceivablesTurnover"]),
      inventoryTurnover: Financial.fromJson(json["InventoryTurnover"]),
      fixedAssetsTurnover: Financial.fromJson(json["FixedAssetsTurnover"]),
      assetTurnover: Financial.fromJson(json["AssetTurnover"]),
      bookValuePerShare: Financial.fromJson(json["BookValuePerShare"]),
      capSpending: Financial.fromJson(json["CapSpending"]),
      dividends: Financial.fromJson(json["Dividends"]),
      earningsPerShare: Financial.fromJson(json["EarningsPerShare"]),
      freeCashFlowPerShare: Financial.fromJson(json["FreeCashFlowPerShare"]),
      freeCashFlow: Financial.fromJson(json["FreeCashFlow"]),
      grossMargin: Financial.fromJson(json["GrossMargin"]),
      netIncome: Financial.fromJson(json["NetIncome"]),
      operatingCashFlow: Financial.fromJson(json["OperatingCashFlow"]),
      operatingIncome: Financial.fromJson(json["OperatingIncome"]),
      operatingMargin: Financial.fromJson(json["OperatingMargin"]),
      payoutRatio: Financial.fromJson(json["PayoutRatio"]),
      revenue: Financial.fromJson(json["Revenue"]),
      shares: Financial.fromJson(json["Shares"]),
      workingCapital: Financial.fromJson(json["WorkingCapital"]),
      cogs: Financial.fromJson(json["COGS"]),
      grossSalesMargin: Financial.fromJson(json["GrossSalesMargin"]),
      ebtMargin: Financial.fromJson(json["EBTMargin"]),
      netIntIncOther: Financial.fromJson(json["NetIntIncOther"]),
      otherSalesMargins: Financial.fromJson(json["OtherSalesMargins"]),
      operationSalesMargin: Financial.fromJson(json["OperationSalesMargin"]),
      researchAndDevelopment: Financial.fromJson(json["ResearchAndDevelopment"]),
      sga: Financial.fromJson(json["SGA"]),
      salesRevenue: Financial.fromJson(json["SalesRevenue"]),
      assetTurnoverAverage: Financial.fromJson(json["AssetTurnoverAverage"]),
      financialLeverageAverage: Financial.fromJson(json["FinancialLeverageAverage"]),
      interestCoverage: Financial.fromJson(json["InterestCoverage"]),
      netMargin: Financial.fromJson(json["NetMargin"]),
      returnOnAssets: Financial.fromJson(json["ReturnOnAssets"]),
      returnOnInvestedCapital: Financial.fromJson(json["ReturnOnInvestedCapital"]),
      taxRate: Financial.fromJson(json["TaxRate"]),
      revenueFiveYearAverage: Financial.fromJson(json["RevenueFiveYearAverage"]),
      revenueThreeYearAverage: Financial.fromJson(json["RevenueThreeYearAverage"]),
      revenueTenYearAverage: Financial.fromJson(json["RevenueTenYearAverage"]),
      revenueYearOverYear: Financial.fromJson(json["RevenueYearOverYear"]),
      operatingIncomeFiveYearAverage: Financial.fromJson(json["OperatingIncomeFiveYearAverage"]),
      operatingIncomeTenYearAverage: Financial.fromJson(json["OperatingIncomeTenYearAverage"]),
      operatingIncomeThreeYearAverage: Financial.fromJson(json["OperatingIncomeThreeYearAverage"]),
      operatingIncomeYearOverYear: Financial.fromJson(json["OperatingIncomeYearOverYear"]),
      netIncomeFiveYearAverage: Financial.fromJson(json["NetIncomeFiveYearAverage"]),
      netIncomeTenYearAverage: Financial.fromJson(json["NetIncomeTenYearAverage"]),
      netIncomeThreeYearAverage: Financial.fromJson(json["NetIncomeThreeYearAverage"]),
      netIncomeYearOverYear: Financial.fromJson(json["NetIncomeYearOverYear"]),
      epsFiveYearAverage: Financial.fromJson(json["EPSFiveYearAverage"]),
      epsTenYearAverage: Financial.fromJson(json["EPSTenYearAverage"]),
      epsThreeYearAverage: Financial.fromJson(json["EPSThreeYearAverage"]),
      epsYearOverYear: Financial.fromJson(json["EPSYearOverYear"]),
      operatingCashFlowYearOverYear: Financial.fromJson(json["OperatingCashFlowYearOverYear"]),
      freeCashFlowYearOverYear: Financial.fromJson(json["FreeCashFlowYearOverYear"]),
      capExSales: Financial.fromJson(json["CapExSales"]),
      freeCashFlowSales: Financial.fromJson(json["FreeCashFlowSales"]),
      freeCashFlowNetIncome: Financial.fromJson(json["FreeCashFlowNetIncome"]),
    );
  } // factory TimeSeriesMetaData.fromJson

  @override
  String toString() {
    return 'RatiosModel: {cashAndShortTermInvestments = $cashAndShortTermInvestments, '
        'accountsReceivable = $accountsReceivable, inventory = $inventory, '
        'otherCurrentAssets = $otherCurrentAssets, totalCurrentAssets = $totalCurrentAssets, '
        'netPPE= $netPPE, intangibles = $intangibles, otherLongTermAssets = $otherLongTermAssets, '
        'totalAssets = $totalAssets, accountsPayable = $accountsPayable, '
        'shortTermDebt = $shortTermDebt, taxesPayable = $taxesPayable, '
        'accruedLiabilities = $accruedLiabilities, otherShortTermLiabilities = $otherShortTermLiabilities, '
        'totalCurrentLiabilities = $totalCurrentLiabilities, longTermDebt = $longTermDebt, '
        'otherLongTermLiabilities = $otherLongTermLiabilities, totalLiabilities = $totalLiabilities, '
        'totalStockholdersEquity = $totalStockholdersEquity, totalLiabilitiesEquity = $totalLiabilitiesEquity, '
        'currentRatio = $currentRatio, quickRatio = $quickRatio, '
        'financialLeverage = $financialLeverage, debtEquity = $debtEquity, '
        'daysSalesOutstanding = $daysSalesOutstanding, daysInventory = $daysInventory, '
        'payablesPeriod = $payablesPeriod, cashConversionCycle = $cashConversionCycle, '
        'receivablesTurnover = $receivablesTurnover, inventoryTurnover = $inventoryTurnover, '
        'fixedAssetsTurnover = $fixedAssetsTurnover, assetTurnover = $assetTurnover, '
        'bookValuePerShare = $bookValuePerShare, capSpending = $capSpending, dividends = $dividends, '
        'earningsPerShare = $earningsPerShare, freeCashFlowPerShare = $freeCashFlowPerShare, '
        'freeCashFlow = $freeCashFlow, grossMargin = $grossMargin, netIncome = $netIncome, '
        'operatingIncome = $operatingIncome, operatingMargin = $operatingMargin, payoutRatio = $payoutRatio, '
        'payoutRatio = $payoutRatio, revenue = $revenue, shares = $shares, '
        'workingCapital = $workingCapital, cogs = $cogs, grossSalesMargin = $grossSalesMargin, '
        'ebtMargin = $ebtMargin, netIntIncOther = $netIntIncOther, otherSalesMargins = $otherSalesMargins, '
        'operationSalesMargin = $operationSalesMargin, researchAndDevelopment = $researchAndDevelopment, '
        'sga = $sga, salesRevenue = $salesRevenue, assetTurnoverAverage = $assetTurnoverAverage, '
        'financialLeverageAverage = $financialLeverageAverage, interestCoverage = $interestCoverage, '
        'netMargin = $netMargin, returnOnAssets = $returnOnAssets, '
        'returnOnInvestedCapital = $returnOnInvestedCapital, taxRate = $taxRate, '
        'revenueFiveYearAverage = $revenueFiveYearAverage, revenueThreeYearAverage = $revenueThreeYearAverage, '
        'revenueTenYearAverage = $revenueTenYearAverage, revenueYearOverYear = $revenueYearOverYear, '
        'operatingIncomeFiveYearAverage = $operatingIncomeFiveYearAverage, '
        'operatingIncomeTenYearAverage = $operatingIncomeTenYearAverage, '
        'operatingIncomeThreeYearAverage = $operatingIncomeThreeYearAverage, '
        'operatingIncomeYearOverYear = $operatingIncomeYearOverYear, '
        'netIncomeFiveYearAverage = $netIncomeFiveYearAverage, netIncomeTenYearAverage = $netIncomeTenYearAverage, '
        'netIncomeThreeYearAverage = $netIncomeThreeYearAverage, netIncomeYearOverYear = $netIncomeYearOverYear, '
        'epsFiveYearAverage = $epsFiveYearAverage, epsTenYearAverage = $epsTenYearAverage, '
        'epsThreeYearAverage = $epsThreeYearAverage, epsYearOverYear = $epsYearOverYear, '
        'operatingCashFlowYearOverYear = $operatingCashFlowYearOverYear, '
        'freeCashFlowYearOverYear = $freeCashFlowYearOverYear, capExSales = $capExSales, '
        'freeCashFlowSales = $freeCashFlowSales, freeCashFlowNetIncome }';
  } // toString
} // class RatiosModel


import 'package:flutter/material.dart';
import 'dart:convert';
import 'package:http/http.dart' as http;
import 'package:flutter/services.dart' show rootBundle;

import 'package:globalfleas/models/ratios_model.dart';

class Test extends StatefulWidget {
  Test({Key? key/*, this.title*/}) : super(key: key);
  //final String title;
  @override
  _TestState createState() => _TestState();
}

class _TestState extends State<Test> {
  //late Future <RatiosModel> _ratios;

  void initState() {
    super.initState();
    //_ratios = _getRatios();
    _getRatios();
  } // void initState()

  //Future <RatiosModel> _getRatios() async {
  _getRatios() async {
    /*// https://stackoverflow.com/questions/58377795/how-to-pass-headers-in-the-http-post-request-in-flutter
    final response = await http.get(
      Uri.parse('https://services.last10k.com/v1/company/ibm/ratios'),
      // Send authorization headers to the backend.
      headers: {
        'Ocp-Apim-Subscription-Key': 'a22e5957bd6a4f43ac1af306db39c00b'
      },
    );
    print('response: ${response.body}');
    dynamic jsonObject = json.decode(response.body);
    */
    String response = await rootBundle.loadString('assets/last10k_ratios.json');
    dynamic jsonObject = json.decode(response);
    final convertedJsonObject = jsonObject.cast<dynamic, dynamic>();
    print('convertedJsonObject: ${convertedJsonObject["data"]["attributes"]["result"]}');
    Financial financials = Financial.fromJson(convertedJsonObject["data"]["attributes"]["result"]['CashAndShortTermInvestments']);
    print('financials: $financials');
    RatiosModel ratios = RatiosModel.fromJson(convertedJsonObject["data"]["attributes"]["result"]);
      /*convertedJsonObject["data"]["attributes"]["result"].map<RatiosModel>((json) =>
        RatiosModel.fromJson(json)).toList();*/
    print('ratios: $ratios');
    print('ratios[cashAndShortTermInvestments]: ${ratios.cashAndShortTermInvestments}');
    //return ratios;
  } // Future <RatiosModel> _getRatios() async

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Text('test'),
    );
  } // Widget build(BuildContext context)

} 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM