【问题标题】:Show all data from json list显示 json 列表中的所有数据
【发布时间】:2020-10-23 03:07:39
【问题描述】:

我有这个 json -

[
    {
        "id": 1046474266,
        "serviceTag": "5N4H3Z2",
        "orderBuid": 1212,
        "shipDate": "2019-11-29T06:00:00Z",
        "productCode": ">/030",
        "localChannel": "ENTP",
        "productId": "latitude-14-5400-laptop",
        "productLineDescription": "LATITUDE 5400",
        "productFamily": "Unknown",
        "systemDescription": "Latitude 5400",
        "productLobDescription": "Latitude",
        "countryCode": "SE",
        "duplicated": false,
        "invalid": false,
        "entitlements": [
            {
                "itemNumber": "439-18919",
                "startDate": "2019-11-30T00:00:00.22Z",
                "endDate": "2025-02-26T23:59:59.224Z",
                "entitlementType": "INITIAL",
                "serviceLevelCode": "PJ",
                "serviceLevelDescription": "P, ProDeploy Basic Client",
                "serviceLevelGroup": 8
            },
            {
                "itemNumber": "709-16269",
                "startDate": "2019-11-29T00:00:00.405Z",
                "endDate": "2021-02-28T23:59:59.408Z",
                "entitlementType": "INITIAL",
                "serviceLevelCode": "CB",
                "serviceLevelDescription": "Collect and Return Initial with Dates",
                "serviceLevelGroup": 5
            },
            {
                "itemNumber": "723-42456",
                "startDate": "2021-03-01T00:00:00.398Z",
                "endDate": "2024-03-01T23:59:59.402Z",
                "entitlementType": "EXTENDED",
                "serviceLevelCode": "ND",
                "serviceLevelDescription": "C, NBD ONSITE",
                "serviceLevelGroup": 5
            },
            {
                "itemNumber": "723-42453",
                "startDate": "2019-11-29T00:00:00.987Z",
                "endDate": "2021-02-28T23:59:59.991Z",
                "entitlementType": "INITIAL",
                "serviceLevelCode": "ND",
                "serviceLevelDescription": "C, NBD ONSITE",
                "serviceLevelGroup": 5
            }
        ]
    },
    {
        "id": 1039573973,
        "serviceTag": "3Z2N9Y2",
        "orderBuid": 1212,
        "shipDate": "2019-11-01T05:00:00Z",
        "productCode": ">/030",
        "localChannel": "ENTP",
        "productId": "latitude-14-5400-laptop",
        "productLineDescription": "LATITUDE 5400",
        "productFamily": "Unknown",
        "systemDescription": "Latitude 5400",
        "productLobDescription": "Latitude",
        "countryCode": "SE",
        "duplicated": false,
        "invalid": false,
        "entitlements": [
            {
                "itemNumber": "709-16269",
                "startDate": "2019-11-01T00:00:00.723Z",
                "endDate": "2021-02-01T23:59:59.727Z",
                "entitlementType": "INITIAL",
                "serviceLevelCode": "CB",
                "serviceLevelDescription": "Collect and Return Initial with Dates",
                "serviceLevelGroup": 5
            },
            {
                "itemNumber": "723-42453",
                "startDate": "2019-11-01T00:00:00.077Z",
                "endDate": "2021-02-01T23:59:59.08Z",
                "entitlementType": "INITIAL",
                "serviceLevelCode": "ND",
                "serviceLevelDescription": "C, NBD ONSITE",
                "serviceLevelGroup": 5
            },
            {
                "itemNumber": "439-18919",
                "startDate": "2019-11-02T00:00:00.733Z",
                "endDate": "2025-01-29T23:59:59.737Z",
                "entitlementType": "INITIAL",
                "serviceLevelCode": "PJ",
                "serviceLevelDescription": "P, ProDeploy Basic Client",
                "serviceLevelGroup": 8
            },
            {
                "itemNumber": "723-42456",
                "startDate": "2021-02-02T00:00:00.053Z",
                "endDate": "2024-02-02T23:59:59.056Z",
                "entitlementType": "EXTENDED",
                "serviceLevelCode": "ND",
                "serviceLevelDescription": "C, NBD ONSITE",
                "serviceLevelGroup": 5
            }
        ]
    }
]

我想为同一个“serviceTag”显示所有“serviceLevelDescription”

这是我所做的:

]1

所以你可以看到我得到了我需要的一切,但你可以看到我只是从“serviceLevelDescription”得到最后一个值,而不是所有“serviceLevelDescription”

那么我该怎么做才能显示所有“serviceLevelDescription”??

我的代码!!

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

import 'package:dell_warranty/dellTok.dart';
import 'package:dell_warranty/model_json.dart';
import 'package:http/http.dart';

void main() => runApp(new MaterialApp(
      home: new DellW(),
    ));

class DellW extends StatefulWidget {
  @override
  DellWState createState() => new DellWState();
}

class DellWState extends State<DellW> {
  TextEditingController serviceTagg = TextEditingController();
  List<ModelJson> _taggs;
  StreamController _streamController;
  Stream _stream;

  static const String getToken =
      "https://apigtwb2c.us.dell.com/auth/oauth/v2/token";
  static const String apiUrl =
      "https://apigtwb2c.us.dell.com/PROD/sbil/eapi/v5/asset-entitlements";
  static DellTok dellTok;
  static ModelJson modelJson;

  search() async {
    if (serviceTagg.text == null || serviceTagg.text.length == 0) {
      _streamController.add(null);
      return;
    }
    var queryParameters = {
      'access_token': dellTok.accessToken.toString(),
      'servicetags': serviceTagg.text.trim().toString()
    };
    var uri = Uri.https('apigtwb2c.us.dell.com',
        '/PROD/sbil/eapi/v5/asset-entitlements', queryParameters);

    var responseDell = await http.get(uri, headers: {});
    print(responseDell.body);
    _streamController.add(json.decode(responseDell.body));
  }

  @override
  void initState() {
    super.initState();
    _streamController = StreamController();
    _stream = _streamController.stream;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("LOAD"),
        bottom: PreferredSize(
          preferredSize: Size.fromHeight(48.0),
          child: Row(
            children: <Widget>[
              Expanded(
                child: Container(
                  margin: const EdgeInsets.only(left: 12.0, bottom: 8.0),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    borderRadius: BorderRadius.circular(24.0),
                  ),
                  child: TextFormField(
                    controller: serviceTagg,
                    decoration: InputDecoration(
                      hintText: "Search for a word",
                      contentPadding: const EdgeInsets.only(left: 24.0),
                      border: InputBorder.none,
                    ),
                  ),
                ),
              ),
              IconButton(
                icon: Icon(
                  Icons.search,
                  color: Colors.white,
                ),
                onPressed: () {
                  search();
                },
              )
            ],
          ),
        ),
      ),
      body: Container(
        margin: const EdgeInsets.all(8.0),
        child: StreamBuilder(
          stream: _stream,
          builder: (BuildContext ctx, AsyncSnapshot snapshot) {
            if (snapshot.data == null) {
              return Center(
                child: Text("Enter a search word"),
              );
            }

            if (snapshot.data == "waiting") {
              return Center(
                child: CircularProgressIndicator(),
              );
            }

            return ListView.builder(
              itemCount: snapshot.data.length,
              itemBuilder: (BuildContext context, int index) {
                return ListBody(
                  children: <Widget>[
                    Container(
                      color: Colors.grey[300],
                      child: ListTile(
                        leading: snapshot.data[index]["serviceTag"] == null
                            ? null
                            : Text(
                                snapshot.data[index]["serviceTag"],
                              ),
                        title: Text(snapshot.data[index]
                                    ["productLineDescription"] ==
                                null
                            ? null
                            : snapshot.data[index]["productLineDescription"] +
                                ")"),
                      ),
                    ),
                    Container(
                      child: Column(
                        children: [
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(snapshot.data[index]
                                        ["productLobDescription"] ==
                                    null
                                ? null
                                : snapshot.data[index]
                                    ["productLobDescription"]),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: new Text(snapshot.data[index]["entitlements"]
                                        [index]["startDate"] ==
                                    null
                                ? null
                                : snapshot.data[index]["entitlements"][index]
                                    ["startDate"]),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(snapshot.data[index]["entitlements"]
                                        [index]["endDate"] ==
                                    null
                                ? null
                                : snapshot.data[index]["entitlements"][index]
                                    ["endDate"]),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(snapshot.data[index]["entitlements"]
                                        [index]["entitlementType"] ==
                                    null
                                ? null
                                : snapshot.data[index]["entitlements"][index]
                                    ["entitlementType"]),
                          ),
                          Padding(
                            padding: const EdgeInsets.all(8.0),
                            child: Text(snapshot.data[index]["entitlements"]
                                        [index]["serviceLevelDescription"] ==
                                    null
                                ? null
                                : snapshot.data[index]["entitlements"][index]
                                    ["serviceLevelDescription"]),
                          ),
                        ],
                      ),
                    )
                  ],
                );
              },
            );
          },
        ),
      ),
    );
  }
}

【问题讨论】:

    标签: android json flutter dart


    【解决方案1】:

    我可以看到您有一个要显示的数组,您当前的代码只访问一个索引 ([index]["serviceLevelDescription"]),这就是为什么您只显示一个结果。

    您可以创建一个负责填充数组的新函数。然后将权利数组属性传递给该函数 - 像这样 - [index]["entitlements"]

    renderServiceLevelDescription(entitlementsArray) {
      return Row(children: entitlementsArray.map((entitlement) => 
          Flexible(
            child: Text(entitlement["serviceLevelDescription"]), //You can edit this / style as you like
        )
      ).toList());
    }
    

    然后你更新你的卡代码来调用它!

    希望这会有所帮助:)

    【讨论】:

    • 嘿!并感谢您的回复,我是新手,我希望您能帮助我如何处理您的代码?你能告诉我如何调用这个函数吗?非常感谢:)
    • 你好!我试图在类中应用你的代码并从列中调用它(children:[renderServiceLevelDescription(snapshot.data[index]["entitlements"]),我得到了这个错误,以下 NoSuchMethodError 被抛出构建:类'_InternalLinkedHashMap' 没有实例 getter 'serviceLevelDescription'。接收方:_LinkedHashMap len:7 尝试调用:serviceLevelDescription
    • 将 entitlement.serviceLevelDescription 更改为 entitlement["entitlement.serviceLevelDescription"] - 我忘了您访问的不是对象而是字典。
    • 嘿!那没有用!感谢您的帮助:)
    • 嘿!我试图弄清楚如何访问 snapshot.data[index]["entitlements"][index]["startDate"] 在这种情况下,我使用索引访问并索引它不是这样做的权利!如果索引是 2 并且在权利中只有一张地图,那么它是错误的!如果你能帮助我,我不和谐!只是告诉我你是否需要一些时间来帮忙!
    猜你喜欢
    • 2015-12-15
    • 2020-02-08
    • 2016-02-22
    • 2017-01-21
    • 1970-01-01
    • 2012-05-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多