Flutter:如何在其他对象的字幕中添加其他值

时间:2019-11-22 02:44:27

标签: 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) 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"]}'),
        );
      },
    );
  }
}

在副标题中打印/显示更多值(大于1)。

          leading: Icon(Icons.album),
          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"]["stats"]["iexRealtimePrice"]}'),
         // subtitle: Text ( ' ${photos[index].stats["stats"]["dividendYield"]}'),
        );

Json

  
    

{“ SLVO”:{“统计信息” :{“ week52change”:0.047256,“ week52high”:7.48,“ week52low”:6.23,“ marketcap”:null,“员工”:null ,“ day200MovingAvg”:6.88,“ day50MovingAvg”:7.13,“ float”:null,“ avg10Volume”:7454.8,“ avg30Volume”:7927.83,“ ttmEPS”:null,“ ttmDividendRate”:null,“ companyName”:“瑞士信贷X-Links Silver Shares Covered Call 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,” nextEarningsDate ,“ exDividendDate”:null,“ peRatio”:null,“ beta”:-0.06347545711182472},“ quote” :{“ symbol”:“ SLVO”,“ companyName”:“瑞士信贷X-链接白银股票涵盖的电话ETN”,“ primaryExchange”:“ NASDAQ”,“ calculationPrice”:“ close”,“ open”:6.89,“ openTime”:1574346600589,“ cl ose“:6.88,” closeTime“:1574370000242,” high“:6.9,” low“:6.87,” latestPrice“:6.88,” latestSource“:” Close“,” latestTime“:” 11月21日,

  

已添加: &types = 统计,引用和令牌

enter image description here

除了副标题中的价格外,例如:

  

week52change”:0.047256或“市值”:空

Example

1 个答案:

答案 0 :(得分:1)

也许使用Column

  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"]}'),
    ],
  ),
);