Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/dart/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振:如何从附加对象在字幕中添加附加值_Flutter_Dart - Fatal编程技术网

Flutter 颤振:如何从附加对象在字幕中添加附加值

Flutter 颤振:如何从附加对象在字幕中添加附加值,flutter,dart,Flutter,Dart,如何在附加对象的字幕中添加附加值 import 'dart:async'; import 'dart:convert'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:http/http.dart' as http; Future<List<Photo>> fetchPhotos(http.Client client)

如何在附加对象的字幕中添加附加值

import 'dart:async';
import 'dart:convert';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

Future<List<Photo>> fetchPhotos(http.Client client) async {
  final response =
  await client.get('https://cloud.iexapis.com/stable/stock/market/batch?symbols=SLVO,GLDI,SPFF,CNPF,MONY&types=quote,**stats**&token=');

  // Use the compute function to run parsePhotos in a separate isolate.
  return compute(parsePhotos, response.body);
}

// A function that converts a response body into a List<Photo>.
List<Photo> parsePhotos(String responseBody) {
  //final parsed = json.decode(responseBody).cast<Map<String, dynamic>>();

  //return parsed.map<Photo>((json) => Photo.fromJson(json)).toList();

  dynamic Obj = json.decode(responseBody);
  print(Obj.length);
  List<Photo> photoList = [];
  Obj.forEach((k, v,y) => photoList.add(Photo(k,v,y)));

  return photoList;
}

class Photo {
  String symbol;
 dynamic data;
  dynamic stats;
  Photo(this.symbol ,this.data, this.stats);
}

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final appTitle = 'Isolate Demo';

    return MaterialApp(
      title: appTitle,
      home: MyHomePage(title: appTitle),
    );
  }
}

class MyHomePage extends StatelessWidget {
  final String title;

  MyHomePage({Key key, this.title}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(title),
      ),
      body: FutureBuilder<List<Photo>>(
        future: fetchPhotos(http.Client()),
        builder: (context, snapshot) {
          if (snapshot.hasError) print(snapshot.error);

          return snapshot.hasData
              ? PhotosList(photos: snapshot.data)
              : Center(child: CircularProgressIndicator());
        },
      ),
    );
  }
}

class PhotosList extends StatelessWidget {
  final List<Photo> photos;

  PhotosList({Key key, this.photos}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return GridView.builder(
      gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 2,
      ),
      itemCount: photos.length,
      itemBuilder: (context, index) {
        return ListTile(
          leading: Icon(Icons.album),
          subtitle: Column(
            children: [
             // Text( ' ${photos[index].stats["stats"]["dividendYield"]["companyName"]}'),
            //  Text( ' ${photos[index].data["quote"]["iexRealtimePrice"]}' ' ${photos[index].stats["stats"]["dividendYield"]["companyName"]}'),
              Text( ' ${photos[index].data["quote"]["iexRealtimePrice"]}'),
              Text ( ' ${photos[index].stats["stats"]["dividendYield"]}'),
            ],
          ),
         //title: Text(photos[index].symbol),
         //subtitle: Text( ' ${photos[index].stats["stats"]["dividendYield"]["companyName"]}'),
          //subtitle: Text( ' ${photos[index].data["quote"]["iexRealtimePrice"]}' ' ${photos[index].stats["stats"]["dividendYield"]["companyName"]}'),
       // subtitle: Text( ' ${photos[index].data["quote"]["iexRealtimePrice"]}'),
         // subtitle: Text ( ' ${photos[index].stats["stats"]["dividendYield"]}'),
        );
      },
    );
  }
}

Json

{“SLVO”:{“统计数据”:{“week52change”:0.047256,“week52high”:7.48,“week52low”:6.23,“marketcap”:null,“employees”:null,“day200MovingAvg”:6.88,“day50MovingAvg”:7.13,“float”:null,“avg10Volume”:7454.8,“avg30Volume”:7927.83,“ttmEPS”:null,“TTMDidendrate”:null,“companyName”:“瑞士信贷X-Links白银股票套保买入ETN”,“sharesOutstanding”:0,“maxChangePercent”:58.7391,“year5ChangePercent”:-0.4128,“year2ChangePercent”:-0.1369,“year1ChangePercent”:0.047256,“ytdChangePercent”:-0.005789,“month6ChangePercent”:0.074445,“month3ChangePercent”:-0.035112,“month1ChangePercent”:-0.025532,“day30ChangePercent”:-0.035112,“day5ChangePercent”:-0.007225,“nextDividendDate”:null,”dividendYield:null,“nextearningdate”:null,“exDividendDate”:null,“peRatio”:null,“beta”:-0.06347545711182472},“quote”:{“symbol”:“SLVO”,“companyName”:“瑞士信贷X-Links银股备兑看涨ETN”,“primaryExchange”:“纳斯达克”,“计算价格”:“收盘”,“开盘”:6.89,“开盘时间”:157436600589,“收盘时间”:6.88,“收盘时间”“:157437000242,“高”:6.9,“低”:6.87,“最新价格”:6.88,“最新来源”:“关闭”,“最晚时间”:“11月21日

添加: &类型=统计数据,引用和标记

除了副标题中的价格之外,我还想要例如:

第52周更改“:0.047256或marketcap”:空


也许可以使用

  leading: Icon(Icons.album),
  title: Text(photos[index].symbol),
  subtitle: Column(
    children: [
      Text( ' ${photos[index].stats["stats"]["dividendYield"]["companyName"]}'),
      Text( ' ${photos[index].data["quote"]["iexRealtimePrice"]}' ' ${photos[index].stats["stats"]["dividendYield"]["companyName"]}'),
      Text( ' ${photos[index].data["quote"]["stats"]["iexRealtimePrice"]}'),
      Text ( ' ${photos[index].stats["stats"]["dividendYield"]}'),
    ],
  ),
);

@chunhunghan刚刚在api json url中添加了“stats”、quote和token。并且希望将附加值打印到副标题中,因为价格为.I/flutter(28902):══╡ WIDGETS库捕获到异常╞═══════════════════════════════════════════════════════════ I/flatter(28902):在建筑中抛出了以下NoSuchMethodError:I/flatter(28902):在null上调用了方法“[]”。I/flatter(28902):接收方:null I/flatter(28902):尝试调用:[](“stats”)@DevEd看起来像
photos[index].stats
则为空。您必须找出原因。@Abion47感谢您提供原始答案。我添加了完整的代码。您能看一下吗?如果json代码有数据,为什么它会为空。
  leading: Icon(Icons.album),
  title: Text(photos[index].symbol),
  subtitle: Column(
    children: [
      Text( ' ${photos[index].stats["stats"]["dividendYield"]["companyName"]}'),
      Text( ' ${photos[index].data["quote"]["iexRealtimePrice"]}' ' ${photos[index].stats["stats"]["dividendYield"]["companyName"]}'),
      Text( ' ${photos[index].data["quote"]["stats"]["iexRealtimePrice"]}'),
      Text ( ' ${photos[index].stats["stats"]["dividendYield"]}'),
    ],
  ),
);