dart 我想询问如何使用API中现有数据替换本地资产

von4xj4u  于 2022-12-06  发布在  其他
关注(0)|答案(1)|浏览(138)

我想将assets/1.json替换为api URL,因为所需的数据在API中,而不是本地的,我该如何更改它,我已经尝试过了,但仍然出错。谢谢

static Future<Map<String, DataKuliahModel>> getDataKuliah() async {
    String url = Constant.baseURL;
    String token = await UtilSharedPreferences.getToken();
    await Future.delayed(const Duration(seconds: 4));
    // String responseJson = await rootBundle.loadString('assets/1.json');

    Map<String, DataKuliahModel> finalResult = {};
    final response = await http.get(
      Uri.parse(
        '$url/auth/mhs_siakad/perwalian/get_paket',
      ),
      headers: {
        'Authorization': 'Bearer $token',
      },
    );
    final result = jsonDecode(response)['data'] as Map<String, dynamic>;
    result.forEach((key, value) {
      DataKuliahModel dataKuliah = DataKuliahModel.fromMap(value);
      finalResult.addAll({
        key: dataKuliah,
      });
    });
    return finalResult;
  }

h22fl7wq

h22fl7wq1#

我不知道你的API或它的响应,但你可能想把response.body传递给你的jsonDecode

相关问题