【发布时间】:2021-09-21 04:35:28
【问题描述】:
我试图从 REST API 获取数据结果,然后将其显示在 UI 中。 所以一切都很顺利,JSON 被很好地解析,try 和 catch 方法工作正常。 但不知何故,代码无法在 UI 中显示解析结果。 它都没有给我一个错误或异常。 在过去的几天里,我一直在努力达到预期的结果。
这是 JSON 的样子:
{
"data-description": "This api will return an array of objects to be placed in the order status timeline on the second screen",
"order-status": "Success",
"status-objects": [
{
"type": "Payment",
"status": "completed",
"date": "2021-07-02T00:00:00",
"time": "12:00AM"
},
{
"type": "Units Allocated",
"status": "by Axis",
"date": "2021-07-13T00:00:00",
"time": "12:00AM"
}
]
}
为了让问题更清楚,我将附上我的代码sn-ps。
模型类
Transaction transactionFromJson(String str) =>
Transaction.fromJson(json.decode(str));
String transactionToJson(Transaction data) => json.encode(data.toJson());
class Transaction {
Transaction({
required this.dataDescription,
required this.orderStatus,
required this.statusObjects,
});
String dataDescription;
String orderStatus;
List<StatusObject> statusObjects;
factory Transaction.fromJson(Map<String, dynamic> json) => Transaction(
dataDescription: json["data-description"],
orderStatus: json["order-status"],
statusObjects: List<StatusObject>.from(
json["status-objects"].map((x) => StatusObject.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"data-description": dataDescription,
"order-status": orderStatus,
"status-objects":
List<dynamic>.from(statusObjects.map((x) => x.toJson())),
};
}
class StatusObject {
StatusObject({
required this.type,
required this.status,
required this.date,
required this.time,
});
String type;
String status;
DateTime date;
String time;
factory StatusObject.fromJson(Map<String, dynamic> json) => StatusObject(
type: json["type"],
status: json["status"],
date: DateTime.parse(json["date"]),
time: json["time"],
);
Map<String, dynamic> toJson() => {
"type": type,
"status": status,
"date": date.toIso8601String(),
"time": time,
};
}
进行解析和获取的API_Manager 服务类
class API_Manager {
static Future<Transaction> getDetails() async {
var client = http.Client();
var transactions;
try {
var response = await client.get(
Uri.https("https://hereistheurl", "/accounts/test-data/"));
if (response.statusCode == 200) {
var jsonString = response.body;
var jsonMap = jsonDecode(jsonString);
transactions = Transaction.fromJson(jsonMap);
}
} catch (e) {
return transactions;
}
return transactions;
}
}
我想要显示解析后的 JSON 的 UI 组件:
代码
FutureBuilder<Transaction>(
future: API_Manager.getDetails(),
builder: (context, snapshot) {
if (snapshot.hasData) {
var data = snapshot.data!.statusObjects;
return ListView.builder(
itemCount: data.length,
itemBuilder: (context, index) =>
Text('$index : ${data[index].status}'),
);
}
return Text('Something was wrong!');
},
),
我很确定我遗漏了一小段代码来使它工作。 我已经在这段代码上工作了好几天,但无法做到。 我请求你们,请帮助我获得结果或指出我遗漏的代码。
如果您能以任何可能的方式帮助我,我们将不胜感激。
【问题讨论】:
-
能否提供更完整的样本数据集进行测试?
-
还有生成
_transactions的代码 -
您可以将 JSON 用于数据集。这是生成 _transactions 的代码:late Future
_transactions; @override void initState() { _transactions = API_Manager().getDetails(); super.initState(); } -
@ΟυιλιαμΑρκευα 请帮助我理解这一点。
标签: json flutter dart flutter-layout flutter-widget