Meikisihka
Meikisihka

Reputation: 177

how to access API data

I want to GET data API [curriculum], but when I try, the following error occurs, or am I doing something wrong?

ERROR: Exception has occurred. _TypeError (type 'Null' is not a subtype of type 'Map<String, dynamic>')

enter image description here

enter image description here

fetch API

 static Future<Kurikulum> getDetailHeader(String id) async {
    String url = Constant.baseURL;
    String token = await UtilSharedPreferences.getToken();
    final response = await http.get(
      Uri.parse(
        '$url/auth/mhs_siakad/kurikulum/detail/$id',
      ),
      headers: {
        'Authorization': 'Bearer $token',
      },
    );
    print(response.statusCode);
    print(response.body);
    if (response.statusCode == 201) {
      final result = json.decode(response.body) as Map<String, dynamic>;
      Data detailKurikulum = Data.fromJson(result['data']['kurikulum']);
      Kurikulum l = detailKurikulum.mk as Kurikulum;
      return l;
    } else {
      throw Exception();
    }
  }

Upvotes: 0

Views: 35

Answers (1)

sharon
sharon

Reputation: 403

remove result['data']['kurikulum'] to result['data] I hope this helps

if (response.statusCode == 201) {
      final result = json.decode(response.body) as Map<String, dynamic>;
      Data dosen = Data.fromJson(result['data']);
      List<Kurikulum> l = dosen.kurikulum;
      return l;
    } else {
      throw Exception();
    }

Upvotes: 1

Related Questions