5

我试图加载数组 json(没有键的数组)并提取到对象列表中,这是我的示例代码:

我的 APICliet:

Future<List<OnBoardingModel>> fetchOnboarding() async {
    try {
      Response response = await dio.get("api/OnboardingItem");
      return OnBoardingModel.fromJson(response.data) as List;
    } catch (error, stacktrace) {
      throw Exception("Exception occured: $error stackTrace: $stacktrace");
    }
  }

我的对象(使用quicktype生成):

List<OnBoardingModel> onBoardingModelFromJson(String str) => List<OnBoardingModel>.from(json.decode(str).map((x) => OnBoardingModel.fromJson(x)));

String onBoardingModelToJson(List<OnBoardingModel> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class OnBoardingModel {
  OnBoardingModel({
    this.id,
    this.judul,
    this.deskripsi,
    this.urlGambar,
  });

  int id;
  String judul;
  String deskripsi;
  String urlGambar;

  factory OnBoardingModel.fromJson(Map<String, dynamic> json) => OnBoardingModel(
    id: json["id"],
    judul: json["judul"],
    deskripsi: json["deskripsi"],
    urlGambar: json["urlGambar"],
  );

  Map<String, dynamic> toJson() => {
    "id": id,
    "judul": judul,
    "deskripsi": deskripsi,
    "urlGambar": urlGambar,
  };
}

最后我得到了一个错误显示:Exception: Exception occured: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' stackTrace: #0,参考这一行return OnBoardingModel.fromJson(response.data) as List;

你能帮我解决这个案子吗?我谢谢你

更新

我的 json 格式:

[
  {
    "id": 0,
    "judul": "string",
    "deskripsi": "string",
    "urlGambar": "string"
  }
]
4

2 回答 2

23

最后我得到了答案,只需使用这个替换返回:

return (response.data as List)
          .map((x) => OnBoardingModel.fromJson(x))
          .toList();

完整代码:

Future<List<OnBoardingModel>> fetchOnboarding() async {
    try {
      Response response = await dio.get("api/OnboardingItem");
      // if there is a key before array, use this : return (response.data['data'] as List).map((child)=> Children.fromJson(child)).toList();
      return (response.data as List)
          .map((x) => OnBoardingModel.fromJson(x))
          .toList();
    } catch (error, stacktrace) {
      throw Exception("Exception occured: $error stackTrace: $stacktrace");
    }
  }

希望这个答案可以帮助其他新来的客人。

于 2020-06-08T02:14:16.610 回答
1

导入“飞镖:转换”;

List<Welcome> welcomeFromJson(String str) => List<Welcome>.from(json.decode(str).map((x) => Welcome.fromJson(x)));

String welcomeToJson(List<Welcome> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));

class Welcome {
    Welcome({
        this.id,
        this.judul,
        this.deskripsi,
        this.urlGambar,
    });

    int id;
    String judul;
    String deskripsi;
    String urlGambar;

    factory Welcome.fromJson(Map<String, dynamic> json) => Welcome(
        id: json["id"],
        judul: json["judul"],
        deskripsi: json["deskripsi"],
        urlGambar: json["urlGambar"],
    );

    Map<String, dynamic> toJson() => {
        "id": id,
        "judul": judul,
        "deskripsi": deskripsi,
        "urlGambar": urlGambar,
    };
}

最后的欢迎 = welcomeFromJson(jsonString);

于 2020-06-06T06:40:45.070 回答