9

I tried to load array json (array with no key) and fetch into list of object, here is my sample code :

My 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");
    }
  }

My Object (generated using 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,
  };
}

In the end i got an error shown : Exception: Exception occured: type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>' stackTrace: #0, refer to this line return OnBoardingModel.fromJson(response.data) as List;

Could you please help me to solve this case. I thank you

Update

My json format :

[
  {
    "id": 0,
    "judul": "string",
    "deskripsi": "string",
    "urlGambar": "string"
  }
]
Bali Codes
  • 581
  • 1
  • 5
  • 8

2 Answers2

36

Finally i got the answer, just replace the return using this :

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

Complete code :

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");
    }
  }

Hope this answer helps other guests who new in flutter.

Bali Codes
  • 581
  • 1
  • 5
  • 8
1

import 'dart:convert';

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

final welcome = welcomeFromJson(jsonString);

Ankit Tale
  • 1,924
  • 4
  • 17
  • 30