You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter调用自定义网络get方法报_InternalLinkedHashMap类型不匹配错误求解

报错原因
  • 核心错误出在print('here' + response );这行代码:Dart的+字符串拼接要求两边操作数都为String类型,但你封装的get方法返回的是json.decode(response.body)的执行结果,该方法会将接口返回的JSON字符串解析为Map<String, dynamic>类型(即报错信息中的_InternalLinkedHashMap),类型不匹配直接抛出异常。
  • 存在隐藏问题:你的get方法没有处理非200/201状态码的返回逻辑,当接口请求失败时方法会隐式返回null,后续放开注释的状态判断逻辑也可能触发空类型异常。
解决办法

修复当前报错

将打印代码改成以下任意一种写法即可:

  1. 使用Dart字符串插值自动转类型:
print('here$response');
  1. 手动调用toString()方法转字符串后再拼接:
print('here' + response.toString());

优化潜在问题

给get方法补全返回值声明和异常处理,适配空安全规范:

Future<Map<String, dynamic>?> get(String url) async {
    try {
      final String? token =await storage.read(key: "token");
      url = formatter(url);
      var response = await http.get(
        Uri.parse(url),
        headers: {"Authorization": "Bearer $token"},
      );
      if (response.statusCode == 200 || response.statusCode == 201) {
        log.i(response.body);
        return json.decode(response.body) as Map<String, dynamic>;
      }
      log.i(response.body);
      log.i(response.statusCode);
      return null;
    } catch(e) {
      log.e("网络请求异常:$e");
      return null;
    }
}

后续放开业务逻辑时补充空判断,避免空异常:

void checkProfile() async {
    var response = await networkHandler.get('/profile/checkProfile');
    print('here$response');
    if(response != null && response['status'] == true){
      setState(() {
        page = showProfile();
      });
    }else{
      setState(() {
        page = button();
      });
    }
}

内容的提问来源于stack exchange,提问作者Saqib Sattar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 08:36:05