skip to Main Content

I had a problem when I wanted to retrieve data to be displayed on the client, but after I checked again it turned out that the API response was in the form of an object, not an array or list.
so how can i access that data based on the 1,2,3… numbers in the data.
I have discussed with the back end to convert it into an array but they are hesitant to change it, so like it or not I have to take the initiative myself.

{
    "status": "success",
    "code": "200",
    "data": {
        "1": {
            "id": "f732bbb0-a34a-474d-8829-23aa66470e22",
            "id_dosen": "d6aedfb6-cf88-4e89-8365-0f206822a6c4",
            "id_mk": "cb0bced5-a02d-4f46-bd88-6ed61daece10",
            "nidn": null,
            "dosen": "Yudhy",
            "id_kelas_kuliah": "52deb32d-292f-44b9-af69-a90dfc5fbc81",
            "kelas_kuliah": "Pendidikan agama islam III - Sistem Informasi - A",
            "prodi": "Sistem Informasi",
            "kelas": "KARYAWAN",
            "semester": "5",
            "kelompok_kelas": "A",
            "kode": null,
            "sks": 2,
            "jumlah_kelas": 0,
            "matakuliah": "Pendidikan agama islam III ( Islamic Religious Education III ) - A",
            "smt": "2022-2023 GANJIL",
            "bobot_sks": 2,
            "rencana_pertemuan": 14,
            "jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
            "created_at": "2022-09-09 08:14:14",
            "updated_at": "2022-09-09 08:14:14",
            "created_by": "Fahmi Nugraha",
            "updated_by": "Fahmi Nugraha"
        },
        "2": {
            "id": "3573bcf8-bf00-445b-91bb-8362e98f3e70",
            "id_dosen": "d61b7164-cd6c-4bd9-8be8-d2a576790b9c",
            "id_mk": "40f02349-887d-47c2-b190-9c5d62adf738",
            "nidn": null,
            "dosen": "Shadam Hussaeni",
            "id_kelas_kuliah": "fb969bb3-e0d9-47ac-9ede-365c78e38994",
            "kelas_kuliah": "Bahasa inggris III (Conversation) - Sistem Informasi - A",
            "prodi": "Sistem Informasi",
            "kelas": "KARYAWAN",
            "semester": "5",
            "kelompok_kelas": "A",
            "kode": null,
            "sks": 2,
            "jumlah_kelas": 0,
            "matakuliah": "Bahasa inggris III (Conversation) ( English III (Conversation) ) - A",
            "smt": "2022-2023 GANJIL",
            "bobot_sks": 2,
            "rencana_pertemuan": 14,
            "jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
            "created_at": "2022-09-14 08:05:31",
            "updated_at": "2022-09-14 08:05:31",
            "created_by": "Risca Nurzantika",
            "updated_by": "Risca Nurzantika"
        },
        "3": {
            "id": "a12ad665-fc91-44d7-816d-605f51bdcfd7",
            "id_dosen": "e6579b08-7cb0-4ea4-84cb-2f92f8d91d6b",
            "id_mk": "45868d7c-6bcd-4420-9fe7-b60f44e805ce",
            "nidn": null,
            "dosen": "Dr. Partono",
            "id_kelas_kuliah": "5fc85fb1-3057-4d68-af29-b22a5e18eaa2",
            "kelas_kuliah": "Enterprise resource planning(ERP) - Sistem Informasi - A",
            "prodi": "Sistem Informasi",
            "kelas": "KARYAWAN",
            "semester": "5",
            "kelompok_kelas": "A",
            "kode": null,
            "sks": 3,
            "jumlah_kelas": 0,
            "matakuliah": "Enterprise resource planning(ERP) ( Enterprise resource planning(ERP) ) - A",
            "smt": "2022-2023 GANJIL",
            "bobot_sks": 3,
            "rencana_pertemuan": 14,
            "jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
            "created_at": "2022-09-09 08:06:04",
            "updated_at": "2022-09-09 08:06:04",
            "created_by": "Fahmi Nugraha",
            "updated_by": "Fahmi Nugraha"
        },
        "4": {
            "id": "0926b6ac-61fa-4309-bca8-f2deaec22ee6",
            "id_dosen": "dbe7f609-109c-4eb8-be0f-6621461346cb",
            "id_mk": "6bcfc248-b1ff-45da-867e-c4f8ce108e3f",
            "nidn": null,
            "dosen": "Nano Suyatna",
            "id_kelas_kuliah": "8479b48f-de14-499d-9898-43d12b0b29e9",
            "kelas_kuliah": "Kontrol dan audit sistem informasi - Sistem Informasi - A",
            "prodi": "Sistem Informasi",
            "kelas": "KARYAWAN",
            "semester": "5",
            "kelompok_kelas": "A",
            "kode": null,
            "sks": 3,
            "jumlah_kelas": 0,
            "matakuliah": "Kontrol dan audit sistem informasi ( Information system control and audit ) - A",
            "smt": "2022-2023 GANJIL",
            "bobot_sks": 3,
            "rencana_pertemuan": 14,
            "jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
            "created_at": "2022-09-09 08:06:24",
            "updated_at": "2022-09-09 08:06:24",
            "created_by": "Fahmi Nugraha",
            "updated_by": "Fahmi Nugraha"
        },
        "5": {
            "id": "6128d722-2589-4010-a3fe-236876594ba0",
            "id_dosen": "818e059f-4aeb-4c8a-be54-0aece61fb675",
            "id_mk": "1f5cee4f-6543-4067-abf2-88faec0b8163",
            "nidn": null,
            "dosen": "Nova Indrayana Yusman",
            "id_kelas_kuliah": "258c9976-2657-4dae-9239-2b2b2528c4ae",
            "kelas_kuliah": "Statistik komputasi - Sistem Informasi - A",
            "prodi": "Sistem Informasi",
            "kelas": "KARYAWAN",
            "semester": "5",
            "kelompok_kelas": "A",
            "kode": null,
            "sks": 2,
            "jumlah_kelas": 0,
            "matakuliah": "Statistik komputasi ( Computational statistics ) - A",
            "smt": "2022-2023 GANJIL",
            "bobot_sks": 2,
            "rencana_pertemuan": 14,
            "jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
            "created_at": "2022-09-09 08:06:36",
            "updated_at": "2022-09-09 08:06:36",
            "created_by": "Fahmi Nugraha",
            "updated_by": "Fahmi Nugraha"
        },
        "6": {
            "id": "f928be12-c79d-4519-a10d-a2870e379a57",
            "id_dosen": "7329769a-0310-453b-8e4e-5befedd774af",
            "id_mk": "b03740f2-a141-44c3-891d-a46750b94d01",
            "nidn": null,
            "dosen": "Topan Trianto",
            "id_kelas_kuliah": "20279853-74b7-4e0a-8f13-8c1a3b8675fe",
            "kelas_kuliah": "Pemrograman Mobile 2 - Sistem Informasi - A",
            "prodi": "Sistem Informasi",
            "kelas": "KARYAWAN",
            "semester": "5",
            "kelompok_kelas": "A",
            "kode": null,
            "sks": 2,
            "jumlah_kelas": 0,
            "matakuliah": "Pemrograman Mobile 2 ( Mobile programming 2 ) - A",
            "smt": "2022-2023 GANJIL",
            "bobot_sks": 2,
            "rencana_pertemuan": 14,
            "jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
            "created_at": "2022-09-09 08:06:51",
            "updated_at": "2022-09-09 08:06:51",
            "created_by": "Fahmi Nugraha",
            "updated_by": "Fahmi Nugraha"
        },
        "7": {
            "id": "b332e62b-20b7-4041-87cf-b1b2aa9402b5",
            "id_dosen": "57510709-b25b-4b44-abcd-d4c238585daa",
            "id_mk": "66cb1b6d-7c92-4303-a40e-6fc33c650633",
            "nidn": null,
            "dosen": "Yudi Sarip Aripin",
            "id_kelas_kuliah": "31e6dbc5-9096-4210-b6c0-969cd6c1616f",
            "kelas_kuliah": "Rekayasa sistem informasi - Sistem Informasi - A",
            "prodi": "Sistem Informasi",
            "kelas": "KARYAWAN",
            "semester": "5",
            "kelompok_kelas": "A",
            "kode": null,
            "sks": 3,
            "jumlah_kelas": 0,
            "matakuliah": "Rekayasa sistem informasi ( Information systems engineering ) - A",
            "smt": "2022-2023 GANJIL",
            "bobot_sks": 3,
            "rencana_pertemuan": 14,
            "jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
            "created_at": "2022-09-09 08:12:55",
            "updated_at": "2022-09-09 08:12:55",
            "created_by": "Fahmi Nugraha",
            "updated_by": "Fahmi Nugraha"
        },
        "8": {
            "id": "8fea865a-ffd8-4fbb-bce4-58a6c4c28032",
            "id_dosen": "986fab04-4da2-4fdd-8a5c-54f704ff990c",
            "id_mk": "3cdc9ff5-8ca3-4bd9-88f6-84c2f1b3d608",
            "nidn": null,
            "dosen": "Usup Supendi",
            "id_kelas_kuliah": "f26dd9ae-0aa6-42bf-9441-ba5a9afdd024",
            "kelas_kuliah": "Testing & implementasi sistem informasi - Sistem Informasi - A",
            "prodi": "Sistem Informasi",
            "kelas": "KARYAWAN",
            "semester": "5",
            "kelompok_kelas": "A",
            "kode": null,
            "sks": 3,
            "jumlah_kelas": 0,
            "matakuliah": "Testing & implementasi sistem informasi ( Information system testing & implementation ) - A",
            "smt": "2022-2023 GANJIL",
            "bobot_sks": 3,
            "rencana_pertemuan": 14,
            "jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
            "created_at": "2022-09-09 08:12:26",
            "updated_at": "2022-09-09 08:12:26",
            "created_by": "Fahmi Nugraha",
            "updated_by": "Fahmi Nugraha"
        },
        "9": {
            "id": "632152dc-2e65-4b22-8f4d-57448672d4ba",
            "id_dosen": "58b5e16c-1c78-4a15-8366-23163d9b0c71",
            "id_mk": "94a5f157-7c2d-4c58-9f84-8b391fb3e3c6",
            "nidn": null,
            "dosen": "M. Furqon",
            "id_kelas_kuliah": "9c924a24-e73f-4768-9035-0faa625f224c",
            "kelas_kuliah": "KPAM V (Korespondesi) - Sistem Informasi - A",
            "prodi": "Sistem Informasi",
            "kelas": "KARYAWAN",
            "semester": "5",
            "kelompok_kelas": "A",
            "kode": null,
            "sks": 1,
            "jumlah_kelas": 0,
            "matakuliah": "KPAM V (Korespondesi) ( KPAM V (Correspondence) ) - A",
            "smt": "2022-2023 GANJIL",
            "bobot_sks": 1,
            "rencana_pertemuan": 14,
            "jenis_evaluasi": "KOGNITIF/PENGETAHUAN",
            "created_at": "2022-09-09 08:07:37",
            "updated_at": "2022-09-09 08:07:37",
            "created_by": "Fahmi Nugraha",
            "updated_by": "Fahmi Nugraha"
        }
    }
}

3

Answers


  1. in your case we need to add the data‘s nested Map inside a List, so we get the List<Map>:

    Map<String, Map> dataAsMapsInsideMap = respnseResultData;
    List<Map> dataAsMapsInsideList = [];
    dataAsMapsInsideMap.forEach((key, value) {
    dataAsMapsInsideList.add(value);
    });
    print(dataAsMapsInsideList); // will print the List containing the maps
    print(dataAsMapsInsideList.length); // 9
    

    then you can use dataAsMapsInsideList in your project.

    Login or Signup to reply.
  2. You can iterate through map and add values to a new list.

    var response = jsonDecode(apiResponse);
    
    List<YourDartObject> list = [];
    Map<String, dynamic> paketMap = response['data']; 
    
    for (var e in paketMap.values) {
      list.add(Paket.fromJson(e));
    }
    
    Login or Signup to reply.
  3. If your Datum is a list of GetPaket objects you should create list by iterating through json response.

    Future<Datum> getPaketKuliah() async {
        String url = Constant.baseURL;
        String token = await UtilSharedPreferences.getToken();
        final response = await http.get(
          Uri.parse(
            '$url/auth/mhs_siakad/perwalian/get_paket',
          ),
          headers: {
            'Authorization': 'Bearer $token',
          },
        );
        print(response.statusCode);
        print(response.body);
        if (response.statusCode == 200) {
          // return paket.data.entries.first.value;
    
          List<Datum> list = [];
          Map<String, dynamic> paketMap = response['data'];
    
          for (var e in paketMap.values) {
            list.add(Datum.fromJson(e));
          }
          return list[0]; // or any index
    //since you're trying to get only one Datum object
    
        } else {
          throw Exception();
        }
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search