我有这个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
}
]
}
]
并且我希望显示相同“服务标记”的所有“服务级别描述”
这就是我所做的。 我的输出
所以你可以看到我得到了所有我需要的,但是你可以看到我只得到了来自'service leveldescription'的最后一个值,而不是所有'service leveldescription'
那么我如何在显示所有的“服务级别描述”时做到这一点呢??
我的代码!!
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"]),
),
],
),
)
],
);
},
);
},
),
),
);
}
}
我可以看到您有一个想要显示的数组,对于当前代码,您只访问了一个索引([index][“ServiceLevelDescription”]),这就是为什么您只显示了一个结果。
您可以创建一个负责填充数组的新函数。 然后将entitlements数组属性传递给该函数-类似于-[index][“entitlements”]
renderServiceLevelDescription(entitlementsArray) {
return Row(children: entitlementsArray.map((entitlement) =>
Flexible(
child: Text(entitlement.serviceLevelDescription), //You can edit this / style as you like
)
).toList());
}
然后你更新你的卡代码来调用这个!
希望这能有所帮助:)