Json 已解析但无法在 UI flutter 中显示数据

Json got parsed but the cannot display the data in the UI flutter

我试图从 REST API 中获取数据结果,然后将其显示在 UI 中。所以一切顺利 JSON 被解析得很好 try and catch 方法工作正常。但不知何故,代码无法在 UI 中显示解析结果。都没有给我错误或异常。在过去的几天里,我一直在努力获得想要的结果。

型号Class:

import 'dart:convert';

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,
      };
}

This is how the JSON looks like:

    {
        "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"
            }
        ]
    }

API_Manager 解析和获取发生的地方服务 Class

class API_Manager {
  static Future<Transaction> getDetails() async {
    var client = http.Client();
    var transactions;
    try {
      var response = await client.get(
          Uri.https("your api url here"));

      if (response.statusCode == 200) {
        var jsonString = response.body;
        var jsonMap = jsonDecode(jsonString);
        transactions = Transaction.fromJson(jsonMap);
      }
    } catch (e) {
      return transactions;
    }
    return transactions;
  }
}

UI 组件,我想在其中显示已解析的 JSON:

代码

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

我得到的输出是“出了点问题”

我很确定我遗漏了一小段代码来让它工作。我已经在这段代码上工作了好几天,但我做不到。我请求你们,请帮助我获得结果或指出我遗漏的代码。

如果你能以任何可能的方式帮助我,我将不胜感激。

试试这个

var response = await client
          .get(Uri.https("domain", "accounts/test-data/"));

var response = await http
      .get(Uri.parse("domain/accounts/test-data/"));


这个不起作用可能是因为主域部分不应该使用 /,因为它表示子路径,

      var response = await client
          .get(Uri.https("domain/accounts", "test-data/"));