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

Flutter FutureBuilder快照报错:List<dynamic>不兼容FutureOr<List<Product>>

问题分析与解决:type 'List' is not a subtype of type 'FutureOr<List>'

这个错误的核心是类型不匹配,既不是Product模型本身的问题,也不是解析逻辑错误,是返回值的类型推断没跟上预期的类型要求。

你写的body['data'].map((e)=>Product.fromJson(e)).toList(),虽然每个元素都被转换成了Product实例,但默认生成的是List<dynamic>类型,而你的函数声明要求返回Future<List<Product>>,类型不匹配就触发了这个报错。

直接修复方法

把返回语句改成下面两种方式之一即可:

方式一(推荐,类型更安全)

return List<Product>.from(body['data'].map((e) => Product.fromJson(e)));

方式二(强制类型转换)

return body['data'].map((e) => Product.fromJson(e)).toList() as List<Product>;

额外要确认的点

  • 检查Product.fromJson方法是否能正确将JSON对象转换为Product实例,要是这个方法存在逻辑错误,后续可能会出现其他类型相关问题。
  • 确认接口返回的body['data']确实是数组格式,而非单个对象,可以用print(body['data'].runtimeType)打印类型进行验证。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 10:35:23